How to check if Datarow value is null

AleksP picture AleksP · Dec 10, 2014 · Viewed 76.8k times · Source

Tell me please is this is correct way to check NULL in DataRow if need to return a string

 Convert.ToString(row["Int64_id"] ?? "")

Or should be like check with DBNull.Value.

Need to so much more smaller than

if(row["Int64_id"] != DBNull.Value){...}else if{}

Answer

Felipe Oriani picture Felipe Oriani · Dec 10, 2014

Check if the data column is not null with DataRow.IsNull(string columnName)

if (!row.IsNull("Int64_id"))
{
  // here you can use it safety
   long someValue = (long)row["Int64_id"];
}