C# – How to check for null values

cnetsubsonic

I have an integer column that may have a number or nothing assigned to it (i.e. null in the database). How can I check if it is null or not?

I have tried

if(data.ColumnName == null)
{
    ...
}

This doesn't work either (as SubSonic does not use nullable types (when applicable) for ActiveRecord)

if(data.ColumnName.HasValue)
{
    ...
}

If the value stored in the database is 0, then this wouldn't help:

if(data.ColumnName == 0 /* or 0x000? */)
{
    ...
}

The same problem could also occur with DateTime fields as well.

Best Answer

Try:

If (data == System.DBNull)