0

Trying to determine whether a datetime value in datarow is null. i have no idea why this shouldn't work. i've also tried casting to string etc but keep getting same error:

Object cannot be cast from DBNull to other types.

This error doesn't make sense to me, i'm not trying to cast it. I've tried many variants in trying to compare the datarow to DBNull, to no avail.

if(dataRows[0]["Reply_Deadline"] != DBNull.Value)
{
    if(Convert.ToDateTime(dataRows[0]["Reply_Deadline"]) <= DateTime.Now)
    {
        deadlinePast = true;
    }
}

Thanks

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
AncientYouth
  • 471
  • 1
  • 4
  • 12
  • Have you tried the `Convert.IsDBNull(...)` method? Apparently calling `DBNull.Value` is pretty rare. https://msdn.microsoft.com/en-us/library/system.convert.isdbnull(v=vs.110).aspx – Drew Kennedy Jul 21 '15 at 14:43
  • I did, as per the example below, but i'm still getting the same error... – AncientYouth Jul 21 '15 at 14:51

1 Answers1

2

Try

if (!Convert.IsDBNull(dataRows[0]["Reply_Deadline"]))

https://msdn.microsoft.com/en-us/library/system.convert.isdbnull(v=vs.110).aspx

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • here's another alternative: http://stackoverflow.com/questions/4604414/best-way-to-check-if-a-data-table-has-a-null-value-in-it – Steve Greene Jul 21 '15 at 15:03