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