none
Object cannot be cast from DBNull to other types. Error when a null value is read by the Reader

    Question

  • double debit = 0, credit = 0
     SqlCommand cmd = new SqlCommand("select sum(dr_amt),sum(cr_amt) from trans where name='" + com_name.Text + "' and client_name='" + Name.Text + "' ", con);
      con.Open();
       SqlDataReader dr = cmd.ExecuteReader();

         while (dr.Read())
           {

               debit = Convert.ToDouble(dr[0]);//exception
               credit = Convert.ToDouble(dr[1]);

           }

     debit_amt.Text = Convert.ToString(debit);
     credit_amt.Text = Convert.ToString(credit);

    can u modify this code for help

    Tuesday, November 20, 2012 12:46 PM

Answers

  • Hi,

    NULL value like NOTHING is an unknown entity which cannot be manipulated. Any other recipient (variable) associated with NULL will become NULL as well.

    Replace the code in the while loop with the following:

    debit = 0;
    credit = 0;
    if (!object.ReferenceEquals(dr(0), DBNull.Value)) {
    	debit = Convert.ToDouble(dr(0));
    	//exception
    }
    if (!object.ReferenceEquals(dr(1), DBNull.Value)) {
    	credit = Convert.ToDouble(dr(1));
    }

    -------------------------------------------------------------

    Please remember to makr my answer as the answer if it satisfies your need.

    • Marked as answer by dpennnn Wednesday, November 21, 2012 6:47 AM
    Tuesday, November 20, 2012 3:33 PM

All replies

  • double debit = 0, credit = 0
     SqlCommand cmd = new SqlCommand("select sum(dr_amt),sum(cr_amt) from trans where name='" + com_name.Text + "' and client_name='" + Name.Text + "' ", con);
      con.Open();
       SqlDataReader dr = cmd.ExecuteReader();

         while (dr.Read())
           {

               debit = Convert.ToDouble(dr[0]);//exception
               credit = Convert.ToDouble(dr[1]);

           }

     debit_amt.Text = Convert.ToString(debit);
     credit_amt.Text = Convert.ToString(credit);

    can u modify this code for help


    Tuesday, November 20, 2012 12:50 PM
  • hello,

    Best to use assertion

    http://msdn.microsoft.com/en-us/library/system.convert.isdbnull.aspx

    http://msdn.microsoft.com/en-us/library/tckcces5(v=vs.80).aspx

    Just noticed you are using C Sharp while the above is VB.NET the same applies

    Here is a good discussion on C Sharp and dealing with nulls

    http://stackoverflow.com/questions/221582/most-efficient-way-to-check-for-dbnull-and-then-assign-to-a-variable


    KSG


    Tuesday, November 20, 2012 3:17 PM
    Moderator
  • Hi,

    NULL value like NOTHING is an unknown entity which cannot be manipulated. Any other recipient (variable) associated with NULL will become NULL as well.

    Replace the code in the while loop with the following:

    debit = 0;
    credit = 0;
    if (!object.ReferenceEquals(dr(0), DBNull.Value)) {
    	debit = Convert.ToDouble(dr(0));
    	//exception
    }
    if (!object.ReferenceEquals(dr(1), DBNull.Value)) {
    	credit = Convert.ToDouble(dr(1));
    }

    -------------------------------------------------------------

    Please remember to makr my answer as the answer if it satisfies your need.

    • Marked as answer by dpennnn Wednesday, November 21, 2012 6:47 AM
    Tuesday, November 20, 2012 3:33 PM
  • You ask this in the wrong forum, 

    The forum for C# is 

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral

    Be aware that DBNull represent the column type in SQL type database servers and has nothing to do with a non existing object or the value of the object if it is a value type.

    You simple have to check if the value exist and then put a zero in it if you want to use it anyway.

    In both program languages by using DBNull.Value

    The coincidence is that this is one of the occasions where VB and C# have relative slightly different code because of the meaning of Is and = in both program languages.


    Success
    Cor

    Tuesday, November 20, 2012 4:05 PM
  • How to Resolve the error Object cannot be cast from DBNull to other types.

    Reason for the error:

     In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column. Source:MSDN

    Actual Code which I faced error:

    Before changed the code:

            if( ds.Tables[0].Rows[0][0] == null ) //   Which is not working

     

             {

                    seqno  = 1;

              }

            else

            {

                  seqno = Convert.ToInt16(ds.Tables[0].Rows[0][0]) + 1;

             }

    After changed the code:

       if( ds.Tables[0].Rows[0][0] == DBNull.Value ) //which is working properly

            {

                        seqno  = 1;

             }

                else

                {

                      seqno = Convert.ToInt16(ds.Tables[0].Rows[0][0]) + 1;

                 }

    Conclusion: when the database value return the null value, we recommend to use the DBNull class instead of just specifying as a null like in C# language.

    Please let me know your feedback

    Thanks

    Tuesday, June 07, 2016 11:36 AM
  • How to Resolve the error Object cannot be cast from DBNull to other types.

    Reason for the error:

     In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column. Source:MSDN

    Actual Code which I faced error:

    Before changed the code:

            if( ds.Tables[0].Rows[0][0] == null ) //   Which is not working

     

             {

                    seqno  = 1;

              }

            else

            {

                  seqno = Convert.ToInt16(ds.Tables[0].Rows[0][0]) + 1;

             }

    After changed the code:

       if( ds.Tables[0].Rows[0][0] == DBNull.Value ) //which is working properly

            {

                        seqno  = 1;

             }

                else

                {

                      seqno = Convert.ToInt16(ds.Tables[0].Rows[0][0]) + 1;

                 }

    Conclusion: when the database value return the null value, we recommend to use the DBNull class instead of just specifying as a null like in C# language.

    Please let me know your feedback

    Thanks

    Hello,

    Two things, when posting code in this forum, only post VB.NET, not C#. Secondly, we like to keep replies to post done recently, not several months or several years back in time as this post was done in 2012.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, June 07, 2016 12:32 PM
    Moderator
  • But what is written is true Karin, 

    Why do you not move this question to the C# forum?

    It is well stated. 


    Success
    Cor

    Tuesday, June 07, 2016 10:23 PM