Checking for DBNull throws a StrongTypingException

calico-cat picture calico-cat · Jan 14, 2011 · Viewed 20.5k times · Source

I am using a dataset to pull data from a DB. One of the fields in a row is NULL. I know this. However, the following vb.net code throws a StrongTypingException (in the autogenerated get_SomeField() method in the dataset designer):

If Not IsDBNull(aRow.SomeField) Then
'do something
End If

According to documentation and this question it should be fine.

edit: If aRow.SomeField is DBNull.Value Then also returns the same error. Argh.

Answer

MicSim picture MicSim · Jan 14, 2011

Just some additional information: The exception comes because you are using a strongly typed DataSet. StrongTypingException documentation says it:

The exception that is thrown by a strongly typed DataSet when the user accesses a DBNull value.

The usage of strongly typed DataSets is slightly different from that of the untyped ones. With strongly typed DataSets you get automatically some extended/additional methods for your fields that you can call. In your case you would very likely have to call:

If Not aRow.IsSomeFieldNull Then
   'do something
End If