Nullable type and dbNull

Since nullable has been baked into .NET, I had to see if that carried over to ADO.NET. Now I know that datasets’ behavior with Nullable types was so broken that it was removed from the scope of  .NET 2.0, but I wanted to go back and look at my previous tests with dbnull and Nullable types.

Nullable may be “fixed”, but there is still no correlation between them. I know that the fix was for totally different reasons, but I just still had to see.

It is still necessary to test for a dbnull before trying to populate a Nullable<DateTime>, for example.

Any attempts to return date data that is nullable with a datareader.GetDateTime() will give a runtime error. DataReader knows this value is a dbnull, but you will just have to check for yourself and do the little bit of extra work.

nulld Nullable<DateTime>;

‘get some data into a reader and read it

if (myReader.IsDBNull(mycolumn))
{
 nulld=null;
else
 nulld=myReader.GetDateTime(mycolumn);
}

Don’t Forget: www.acehaid.org

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.