Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Specified cast is not valid, when column returns NULL

Reply
Thread Tools

Specified cast is not valid, when column returns NULL

 
 
Web learner
Guest
Posts: n/a
 
      04-06-2006
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 !


 
Reply With Quote
 
 
 
 
Jim Hughes
Guest
Posts: n/a
 
      04-06-2006
VS2005 has a double.TryParse

If the TryParse failes, then set the value to DBNull.Value
"Web learner" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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 !


 
Reply With Quote
 
 
 
 
Flinky Wisty Pomm
Guest
Posts: n/a
 
      04-06-2006
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);
}

 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      04-06-2006
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.

 
Reply With Quote
 
Martin Carpella
Guest
Posts: n/a
 
      04-06-2006
"Bruce Wood" <(E-Mail Removed)> 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
 
Reply With Quote
 
Web learner
Guest
Posts: n/a
 
      04-06-2006
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" <(E-Mail Removed)> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checkbox = null, Specified cast is not valid. =?Utf-8?B?cmtibmFpcg==?= ASP .Net 15 11-01-2007 02:04 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
Begin() applied on empty Vector returns NULL or non null value???? Col C++ 1 04-21-2006 01:12 PM
Error: specified cast is not valid. Why not? Alan Silver ASP .Net 5 02-15-2005 08:08 PM
text column in mysql returns a null or "" value with ASP mike ASP General 1 09-16-2004 01:19 PM



Advertisments