Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Checking for DBNull in DataTable
Message
From
18/03/2004 10:30:10
 
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
00887448
Message ID:
00887506
Views:
9
John,

Nope, you're not being stupid. It's one of my pet peeves about ADO.NET. You do have to check for a null before you can do anything with the contents of a column. But, your syntax doesn't have to be quite as bad as the way you've tried to do it. Try this:
if row(col)= "System.DBNull" Then

instead of the way you had it
if row(col).GetType().ToString = "System.DBNull" Then
One of the things I've done to get around this is to write a static method called GetNonNull(object, string) that will return a default if the object is null. However, I write in C# and I can overload this method to return a string, an integer, a bit, etc ... I'm not sure that VB supports overloaded methods.

~~Bonnie

>I've filled a DataTable with the contents of a number of specified rows, and am having problems displaying the NULL values
>
>Some fields contain Null, or Bit logic
>
>and running something like the following causes an exception
>
>Option Strict is ON
>
>Dim col As DataColumn
>For Each col In dt.Columns
> Console.WriteLine(col.ColumnName & " - " _
> & col.DataType.ToString & " : " & _
> CType(row(col), String))
>Next
>
>It can be traped for using something like the code below:-
>
>if row(col).GetType().ToString = "System.DBNull" Then
> Console.WriteLine(col.ColumnName & " - " _
> & col.DataType.ToString & " : Null")
>Else
> Console.WriteLine(col.ColumnName & " - " _
> & col.DataType.ToString & " : " & _
> CType(row(col), String))
>End IF
>
>
>but this does not seem to me to be the correct solution, what is the correct way for doing this. I've simplified the code that I'm actually using - I also getting similiar problems trapping for "System.Byte[]"
>
>Hope someone can tell me how stupid I am, as you may guess I pretty new to VB.NET
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform