reading null value in a dataset

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monika varshney
    New Member
    • Nov 2008
    • 2

    reading null value in a dataset

    I have to read a database using dataset. My database has some null values.
    My code is working fine when there is a value in the database. But when there is a null value in the database, dataset throws an exception.

    I have to read null value in a variable. I have tried this....

    if (dr["value"] != DBNull.Value)
    {
    abc = Convert.ToDoubl e(dr["value"]);
    MessageBox.Show ("value is:" + abc.ToString()) ;
    }
    else
    {
    abc=null;
    MessageBox.Show ("value is:" + abc.ToString()) ;
    }

    But it gives an error that null can not be assigned to a double variable(here abc is a double variable). How can I read a null value in a variable.
    Can anybody guide me....
  • anijos
    New Member
    • Nov 2008
    • 52

    #2
    Try declaring abc as a nullable variable

    Code:
    double? abc =null;

    Comment

    • Siyodia
      New Member
      • Nov 2008
      • 3

      #3
      try Convert.isDBNul l(database-value-to-check)==true

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        I would have expected your null value to be read as a string by the datareader, hence only needing to test for an empty string.

        Comment

        • rpicilli
          New Member
          • Aug 2008
          • 77

          #5
          Originally posted by monika varshney
          I have to read a database using dataset. My database has some null values.
          My code is working fine when there is a value in the database. But when there is a null value in the database, dataset throws an exception.

          I have to read null value in a variable. I have tried this....

          if (dr["value"] != DBNull.Value)
          {
          abc = Convert.ToDoubl e(dr["value"]);
          MessageBox.Show ("value is:" + abc.ToString()) ;
          }
          else
          {
          abc=null;
          MessageBox.Show ("value is:" + abc.ToString()) ;
          }

          But it gives an error that null can not be assigned to a double variable(here abc is a double variable). How can I read a null value in a variable.
          Can anybody guide me....
          There some way to do that.

          The easiest is to put your code inside a Try Cath constrution

          Try
          your code
          Catch Ex
          verify if was throwed a dbnull exception
          if this is the case make abc = 0 or other value
          End


          I hope this help you

          Rpicilli

          Comment

          • mldisibio
            Recognized Expert New Member
            • Sep 2008
            • 191

            #6
            Just to clarify:
            Your check (dr["value"]!= DbNull.Value) is a correct way of checking for a null value from the database, and is an excellent practice.
            [The other way is "dr[0].IsDbNull" if you know the column index.]

            The error that null cannot be assigned has nothing to do with your database values. It is only because the double "abc" is a value type. Value types have 0 or false as their default values and cannot be assigned to null. Only reference types (objects such as a DataReader or any other class) can be assigned null directly.

            So your option is, as anijos said, declare abc as a Nullable double, which was a new Type added to the Framework in 2.0 for the most part to accomodate null database values. It basically says: I have a double which either has some legitimate value, or is undefined. Beware that using this option brings some extra work. Read best practices for using Nullable types: Nullable Types

            The other option is to leave abc as a normal double, but assign it a value that your code knows equates to a null in the database, such as 0, -1 or Double.MinValue . However, if all those are legitimate values from your data field, then you must find a work around (such as nullable) that signifies the source column is null.

            Comment

            • mldisibio
              Recognized Expert New Member
              • Sep 2008
              • 191

              #7
              P.S. kenobewan:
              Originally posted by kenobewan
              I would have expected your null value to be read as a string by the datareader, hence only needing to test for an empty string.
              Using the "dataReader["columnName "] syntax to pull a value returns the value boxed as an object. So if the source query pulls the field as a numeric type, it will be a boxed numeric.

              For any datatype, even if varchar or such, if the column value is null, it is pulled as DbNull.Value, not as a string.

              :) Mike

              Comment

              • kenobewan
                Recognized Expert Specialist
                • Dec 2006
                • 4871

                #8
                Originally posted by mldisibio
                P.S. kenobewan:


                Using the "dataReader["columnName "] syntax to pull a value returns the value boxed as an object. So if the source query pulls the field as a numeric type, it will be a boxed numeric.

                For any datatype, even if varchar or such, if the column value is null, it is pulled as DbNull.Value, not as a string.

                :) Mike
                They say that you learn something new everyday, good response :).

                Comment

                Working...