Mike Wills
Mike Wills

Reputation: 21275

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?

Upvotes: 3

Views: 2566

Answers (3)

Tyler
Tyler

Reputation: 514

Assuming you're using .NET, there are SqlTypes that can be used in a situation like this.

Upvotes: 0

Michael Petrotta
Michael Petrotta

Reputation: 60972

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

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

Upvotes: 6

David
David

Reputation: 73594

There's a reference here.

or possibly, can you modify your query to ensure the results are not null by using the IsNull operator?

Select (IsNull, SomeDateField, GetDate())

Upvotes: 1

Related Questions