Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6777509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:09:23+00:00 2026-05-26T16:09:23+00:00

I am trying to return a value from a SQL database. However everytime I

  • 0

I am trying to return a value from a SQL database. However everytime I execute the following method I get an error stating, “InvalidCastException was unhandled. Object cannot be cast from DBNull to other types.”

Any light you could shed on this would be greatly appreciated. The method I am using is as follows.

Thanks.

    public static int ScrapTotal2(string prdTypeV, string startDateV, string prtCodeV)
    {
        int scrapTotal2;

        SqlParameter prdType = new SqlParameter("@prdType", SqlDbType.VarChar);
        prdType.Value = prdTypeV;
        SqlParameter startDate = new SqlParameter("@startDate", SqlDbType.VarChar);
        startDate.Value = startDateV;
        SqlParameter prtCode = new SqlParameter("@prtCode", SqlDbType.VarChar);
        prtCode.Value = prtCodeV;

        SqlCommand scrapTotal2SC = new SqlCommand("SELECT SUM([QTY_SCRP]) FROM [TBL_PRDMST] WHERE [PRD_CODE] LIKE @prdType AND [PRD_DATE] = @startDate AND [PRT_CODE] LIKE @prtCode", DataAccess.myConnection);
        scrapTotal2SC.Parameters.Add(prdType);
        scrapTotal2SC.Parameters.Add(startDate);
        scrapTotal2SC.Parameters.Add(prtCode);

        DataAccess.myConnection.Open();
        scrapTotal2 = Convert.ToInt32(scrapTotal2SC.ExecuteScalar());
        DataAccess.myConnection.Close();

        return scrapTotal2;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:09:24+00:00Added an answer on May 26, 2026 at 4:09 pm

    Your SELECT statement returns NULL value. You have to check if it is null first:

    object result = scrapTotal2SC.ExecuteScalar();
    if (result == DBNull.Value) 
    { 
        /* write your code */ 
    }
    else
    {
        scrapTotal2 = Convert.ToInt32(result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to return a string value from a method inside my script tag
I'm trying to return a value from (transport) to the calling function, but can't
Im trying to return the value that a $ajax call returns, from a function
I am trying to get serialized results from a WCF service from database using
Bumped into a strange situation here. Trying to extract cars from a sql database
I'm simply trying to get data from two sql server db tables using ado.net
I am trying to log every SQL statement executed from my scripts. However I
I am trying to get the data from database by using the below code.....
I am trying to use Linq2Sql to return all rows that contain values from
I'm trying to find a way to return the value of an array's parent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.