Search code examples
c#datetimenulldbnull

How do I handle a DBNull DateTime field coming from the SQL Server?


I am getting this error when I retrieve a row with a null DataTime field:

'srRow.Closed_Date' threw an exception of type 'System.Data.StrongTypingException'

How do I properly handle these?


Solution

  • You can check for a null value in that column before retrieving the value.

    if (!srRow.IsClosed_DateNull())
    {
      myDate = srRow.Closed_Date;
    }