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 .n
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
}
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:
if (table.rows[0].IsNull(0))
{
//Whatever I want to do
}
And although the function is called IsNull
it really compares with DbNull
(which is exactly what you need).
What if I want to check for DbNull but I don't have a DataRow? Use Convert.IsDBNull.
row.IsNull("column")
System.Convert.IsDbNull][1](table.rows[0][0]);
IIRC, the (table.rows[0][0] == null)
won't work, as DbNull.Value != null;
Just check for
if(table.rows[0][0] == null)
{
//Whatever I want to do
}
or you could
if(t.Rows[0].IsNull(0))
{
//Whatever I want to do
}
If we are using EF and reading the database element in while loop then,
using( var idr = connection, SP.......)
{
while(idr.read())
{
if(String.IsNullOrEmpty(idr["ColumnNameFromDB"].ToString())
//do something
}
}