Link to home
Start Free TrialLog in
Avatar of Nigel_Taylor
Nigel_Taylor

asked on

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
Avatar of Jason Evans
Jason Evans
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi there.

Use System.DBNull.Value instead.

Jas.
Avatar of Nigel_Taylor
Nigel_Taylor

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect.

Thanks for the quick response.

Regards,

Nigel
Or you could try

if (Convert.ToDateTime(myValue).Equals(System.DBNull.Value)