Checking for NULL data

  • Thread starter Thread starter James Autry
  • Start date Start date
J

James Autry

Does null check have to explicitly be performed on every nullable value from
a DataReader return. ie.
for:

DataReader dr = sqlCommand.ExecuteReader()
while( dr.Read()
{
int intTestValue = dr.GetInt32( dr.GetOrdinal("TestValue"));
}

This will cause an exception if value is null, so do I have to do a special
check for null on every return?? Or is there a more efficient way of doing
this?

Thanks
 
Hi James,

Yes, you need to check for Null for each specific value, since you never
know which column could contain it
 
Yep, you've got to check it each time. While there are some serious problem
with using the following approach with a Datatable b/c of rowstate issues,
you may want to use the ISNULL function on the SQL side. It's more verbose
over there, but you can be sure you don't get any nulls. You can however
create a function for each of the reader's Getxxx methods rather each of the
Getxxx method that are being employed in your query. If you make a class
out of it, you can use static/shared methods and be done with it
http://www.knowdotnet.com/articles/handlingnullvalues.html

The other alternative is a try/catch around each Getxxx invocation which is
probably the most verbose and intrusive approach there would be.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 
Back
Top