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.
Just check for
or you could
IIRC, the
(table.rows[0][0] == null)
won't work, asDbNull.Value != null;
row.IsNull("column")
Just use DataRow.IsNull. It has overrides accepting a column index, a column name, or a DataColumn object as parameters.
Example using the column index:
And although the function is called
IsNull
it really compares withDbNull
(which is exactly what you need).What if I want to check for DbNull but I don't have a DataRow? Use Convert.IsDBNull.
Use DBNull.Value.Equals on the object without converting it to a string.
Here's an example:
If we are using EF and reading the database element in while loop then,