Best way to check if column returns a null value (from database to .net application)

soldieraman picture soldieraman · Jan 7, 2010 · Viewed 127.2k times · Source

I have a table with a DateTime column the column can have NULL values

Now I connect to the database using an ODBC connection and get the value into a DataTable in .net / c#.

I am able to check it for NULL by going

if(String.IsNullOrEmpty(table.rows[0][0].ToString())
{
     //Whatever I want to do
}

Is String.IsNullOrEmpty the correct way to check for null values.

Answer

jball picture jball · Jan 7, 2010

Use DBNull.Value.Equals on the object without converting it to a string.

Here's an example:

   if (! DBNull.Value.Equals(row[fieldName])) 
   {
      //not null
   }
   else
   {
      //null
   }