DataTable cannot equal DBNull

I've seen this a couple of times in code, so it's worth mentioning.  There seems to be some confusion around what DBNull is supposed to be used for.  DBNull is meant to test field values of a DataTable for a value of NULL.  If you're doing something like

If DBNull(myDataTable) = False Then
 ' do something with the DataTable
End If

Then you're setting yourself up for a possible exception. DBNull on anything other than a proper DataTable field will always return false.

Consider the following test

Imports System.Data
Imports NUnit.Framework
<TestFixture()> _
Public Class DBNullTest
    <Test()> _
    Public Sub CanDataTableBeIsDBNull()
        Dim dt As DataTable = Nothing
        Assert.IsTrue(IsDBNull(dt))
    End Sub
End Class

CanDataTableBeIsDBNull will fail because dt is a DataTable type and not a field value from the DataTable.  So if you have to check to see if a DataTable is a valid object after an operation to populate the DataTable, use a comparison with Nothing instead:

    <Test()> _
    Public Sub CanDataTableBeNothing()
        Dim dt As DataTable = Nothing
        Assert.IsTrue(IsNothing(dt))
    End Sub

Posted by: Jeff
Posted on: 1/17/2010 at 1:37 AM
Categories: VB.NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (3) | Post RSSRSS comment feed

Comments

auto &amp; general United States

Sunday, April 18, 2010 1:51 PM

auto &amp; general

I was very impressed. Thank you dear. You are very good. The article was nice to read and full of meaning full of knowledge

Rapidshare United States

Tuesday, April 27, 2010 10:37 PM

Rapidshare

I\'m happy I found this blog, I couldnt discover any info on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, i\'m always look for people to check out my site. Please stop by and leave a comment sometime!

iphone tips United States

Thursday, April 29, 2010 7:42 PM

iphone tips

Like a lot of stuff Apple, the iPad launchas galvanized those who believe it’s the “next big thing” from those who trust it’s the “latest stink” — but what about those who, you know, have actually spent time using the thing, and now had a chance to consider it a bit? Sure, some are no doubt Apple enthusiasts easily impressed, while others are jaded journalists almost unattainable to impress. For a mix of both, chase on after the break. Daring Fireball’s John Gruber has two long posts up about the iPad, and wins the cleverest analogy award hands-down, comparing the Mac to manual and the iPad to automatic transmissions. Car enthusiasts (and genuine experts like race car drivers) still drive cars with manual transmissions. They offer more control; they’re more efficient. But the vast majority of cars sold today are automatics. So too it’ll be with computers. Ultimately, the vast majority will be like the iPad in terms of the degree to which the underlying computer is abstracted away. Manual computers, like the Mac and Windows PCs, will slowly shift from the standard to the niche, something of consciousness only to experts and enthusiasts and developers.

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading