11

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#?

3 Answers 3

19

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Great thank you, completely forgot about "DBNull.Value" - it's been a while since I've manipulated data sets
I wonder why they designed it this way instead of just using null?
@k.rob Because there was no Nullable<T> in .NET 1.0, so there was no way to represent an Int32 value of "null".
you can also use Convert.IsDBNull(value)
-2
try
{
    if (DT.Rows[0][0] != null)
    {      
      //your code
    }    
}    
catch    
{    
    MessageBox.Show("AUTHANICATION FAILED.");    
}

Comments

-2

Please use:

dataTable.Select("FieldName is NULL")

this will give you the DataRows with null values in FieldName column.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.