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

2
  • Have you tried the Convert.IsDBNull(...) method? Apparently calling DBNull.Value is pretty rare. msdn.microsoft.com/en-us/library/… Commented Jul 21, 2015 at 14:43
  • I did, as per the example below, but i'm still getting the same error... Commented Jul 21, 2015 at 14:51

1 Answer 1

2

Try

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

here's another alternative: stackoverflow.com/questions/4604414/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.