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.
Just some additional information: The exception comes because you are using a strongly typed DataSet. StrongTypingException documentation says it:
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:
There is a neat way of going around it. But you need to be aware of consequences.
To prevent exception of occuring you can change in DataSet field property NullValue to either "Null" or "Empty" (whatever suits your needs). Default is set to "Throw Exception".
For a reference look here: msdn documentation
Good luck.
This question is old but what i'm adding doesn't seem to be in any of the other answers.
If you use
This will not throw an exception even if you're using a strongly typed dataset
Try this: aRow.IsSomeFieldNull
The difference is that in the related question it is talking about an untyped value (i.e.
object
) via an indexer. When you go via.SomeField
, the type is already included - so this could beint
etc. And it wouldn't make sense to tryIsDBNull
on anint
, as anint
can never beDBNull
.Essentially the
SomeField
is a wrapper for (excuse the C# accent...)I'm not a huge
DataTable
person, but you could try checking it by name/index/column; or marking the column as nullable so that it isNullable<int>
(in the example above).