Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / April 2006

Tip: Looking for answers? Try searching our database.

Specified cast is not valid, when column returns NULL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Web learner - 06 Apr 2006 01:30 GMT
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 - 06 Apr 2006 01:41 GMT
VS2005 has a double.TryParse

If the TryParse failes, then set the value to DBNull.Value
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 !
Flinky Wisty Pomm - 06 Apr 2006 01:46 GMT
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);
}
Bruce Wood - 06 Apr 2006 04:46 GMT
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.
Martin Carpella - 06 Apr 2006 07:18 GMT
> 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
Web learner - 06 Apr 2006 19:26 GMT
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" <brucewood@canada.com> 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
[quoted text clipped - 3 lines]
> In C# 2.0 there are nullable types, but I'm not sure how they are at
> mediating between DbNull and null.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.