views:

4565

answers:

1

I have a DataTable resultSet; - I'm trying to check fields for null, but get an '{}' (empty-set ?) object back. Searches involving "{}" aren't yielding any appropriate solutions.

This is the code that isn't working as expected when the "fk_id" field is null:

if (resultSet.Rows[0].ItemArray[resultSet.Columns.IndexOf("fk_id")] == null)
{
   //never reaches here
}

Note: using an int index instead of the Columns.IndexOf() isn't the issue.

Also does the "{}" have some other name in C#?

+4  A: 

To check a column for DBNull in a DataSet, you can use the IsNull method:

if (resultSet.Rows[0].IsNull("fk_id"))

Your comparison against null is probably failing because DataSets don't use null to represent a "database NULL" value - they use DBNull.Value. If you need your code to work the way you've presented it, try this:

if (resultSet.Rows[0].ItemArray[resultSet.Columns.IndexOf("fk_id")] == DBNull.Value)
Matt Hamilton
Great thank you, completely forgot about "DBNull.Value" - it's been a while since I've manipulated data sets
Nick Josevski
I wonder why they designed it this way instead of just using null?
Kimball Robinson
@k.rob Because there was no Nullable<T> in .NET 1.0, so there was no way to represent an Int32 value of "null".
Matt Hamilton
@Matt Thanks. That helps.
Kimball Robinson
@Matt you can also use Convert.IsDBNull(value)
Kimball Robinson