Specified cast is not valid, when column returns NULL

Discussion in 'ASP .Net' started by Web learner, Apr 6, 2006.

  1. Web learner

    Web learner Guest

    while (dr.Read())
    //dr is an instance of sqlDataReader
    {
    double minAirTemp = (double)(Single)dr["minAirTemp"];
    // minAirTemp is column in SQLExpress table (data type: real, allows nulls)
    }

    The above code works. But when there are NULL, I get the following error
    System.InvalidCastException: Specified cast is not valid

    To solve this problem, I spent a day on Google. Finally I got a hint from http://www.codeproject.com/cs/database/SmartReader.asp and replaced the code line in while{} as follows:

    double minAirTemp1 = (dr.IsDBNull(dr.GetOrdinal("minAirTemp1")))
    ? 9999 : double.Parse(dr["minAirTemp1"].ToString()); //9999 to respresent nulls

    This works for me for now. But just wondering, whether there is a better method to deal with such a situation????

    Can someone be kind to point me to some suitable reference !

    Thanks !
     
    Web learner, Apr 6, 2006
    #1
    1. Advertising

  2. Web learner

    Jim Hughes Guest

    VS2005 has a double.TryParse

    If the TryParse failes, then set the value to DBNull.Value
    "Web learner" <> wrote in message
    news:...
    while (dr.Read())
    //dr is an instance of sqlDataReader
    {
    double minAirTemp = (double)(Single)dr["minAirTemp"];
    // minAirTemp is column in SQLExpress table (data type: real, allows
    nulls)
    }

    The above code works. But when there are NULL, I get the following error
    System.InvalidCastException: Specified cast is not valid

    To solve this problem, I spent a day on Google. Finally I got a hint from
    http://www.codeproject.com/cs/database/SmartReader.asp and replaced the code
    line in while{} as follows:

    double minAirTemp1 = (dr.IsDBNull(dr.GetOrdinal("minAirTemp1")))
    ? 9999 : double.Parse(dr["minAirTemp1"].ToString()); //9999 to respresent
    nulls
    This works for me for now. But just wondering, whether there is a better
    method to deal with such a situation????
    Can someone be kind to point me to some suitable reference !
    Thanks !
     
    Jim Hughes, Apr 6, 2006
    #2
    1. Advertising

  3. I have a big class called SqlHelper full of methods like:

    public static decimal GetReaderDecimal(IDataRecord dr, int column,
    decimal ifNull)
    {
    return dr.IsDBNull(column)? ifNull : dr.GetDecimal(column);
    }
     
    Flinky Wisty Pomm, Apr 6, 2006
    #3
  4. Web learner

    Bruce Wood Guest

    I did roughly the same thing in C# 1.1: write a static helper method to
    which you pass the row, the column name, and the default value, and it
    returns the value. I had one for each primitive type, including one for
    strings.

    In C# 2.0 there are nullable types, but I'm not sure how they are at
    mediating between DbNull and null.
     
    Bruce Wood, Apr 6, 2006
    #4
  5. "Bruce Wood" <> writes:

    > In C# 2.0 there are nullable types, but I'm not sure how they are at
    > mediating between DbNull and null.


    In C# 2.0 you can use the "as" operator in conjunction with nullable
    types, e.g.:

    IDataReader r = ...;
    int? val = r["MyColumn"] as int?;

    This will gracefully handle the case that r["MyColumn"] contains a
    DbNull.

    Best regards,
    Martin
     
    Martin Carpella, Apr 6, 2006
    #5
  6. Web learner

    Web learner Guest

    For a beginner like me,the whole scenario is overwhelmingly confusing. It is
    hard to figure out what is legacy and what is cutting-edge elegant methods.

    Can someone be kind to point out to latest tutorial material on this topic?
    Please refer to my original messsage.

    Thanks !


    "Bruce Wood" <> wrote in message .
    >I did roughly the same thing in C# 1.1: write a static helper method to
    > which you pass the row, the column name, and the default value, and it
    > returns the value. I had one for each primitive type, including one for
    > strings.
    >
    > In C# 2.0 there are nullable types, but I'm not sure how they are at
    > mediating between DbNull and null.
    >
     
    Web learner, Apr 6, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ajit
    Replies:
    1
    Views:
    3,622
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  2. Floela
    Replies:
    3
    Views:
    578
    Jon Skeet
    Jul 10, 2003
  3. Alan Silver
    Replies:
    5
    Views:
    2,594
    Kevin Spencer
    Feb 15, 2005
  4. vizlab
    Replies:
    3
    Views:
    4,222
    Michael Bar-Sinai
    Oct 17, 2007
  5. =?Utf-8?B?cmtibmFpcg==?=

    Checkbox = null, Specified cast is not valid.

    =?Utf-8?B?cmtibmFpcg==?=, Oct 25, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    3,353
    =?Utf-8?B?cmtibmFpcg==?=
    Nov 1, 2007
Loading...

Share This Page