calico-cat calico-cat - 2 months ago 6 Question

Checking for DBNull throws a StrongTypingException

I am using a dataset to pull data from a DB. One of the fields in a row is

. I know this. However, the following code throws a
(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.

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


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 be int etc. And it wouldn't make sense to try IsDBNull on an int, as an int can never be DBNull.

Essentially the SomeField is a wrapper for (excuse the C# accent...)

public int SomeField {
    get { return (int) this["someFieldName"]; }
    set { this["someFieldName"] = value; }

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 is Nullable<int> (in the example above).