ASP.NET C# DBNull Check

Hi Guys,

In ASP.NET c# I need to do an IF to statement to check if a field is null or not. The field is a datetime field. Can anyone see what I am doing wrong?

  if (Convert.ToDateTime(userData["ConnectionDateTime"]) == System.DBNull)

I am am getting the error System.DBNull is a 'type', which is not valid in the given context.

Hope you can help.

Regards,

Nigel
Nigel_TaylorAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
hongjunConnect With a Mentor Commented:
try this

if (userData["ConnectionDateTime"] == System.DBNull.Value)
0
 
Jason EvansSenior Software DeveloperCommented:
Hi there.

Use System.DBNull.Value instead.

Jas.
0
 
Nigel_TaylorAuthor Commented:
Cool,

I am getting the following error now on the same line.

Operator '==' cannot be applied to operands of type 'System.DateTime' and 'System.DBNull'

Regards,

Nigel
0
 
Nigel_TaylorAuthor Commented:
Perfect.

Thanks for the quick response.

Regards,

Nigel
0
 
Jason EvansSenior Software DeveloperCommented:
Or you could try

if (Convert.ToDateTime(myValue).Equals(System.DBNull.Value)
0
All Courses

From novice to tech pro — start learning today.