Tuesday, July 13, 2010

C#: Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type.

Today I got "Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type." error in the C# application. After my trace, it is caused by this line:

DateTime? moveOutDate = dt.Rows[0].Field("MoveOutDate");

You should make it as:

DateTime? moveOutDate = dt.Rows[0].Field("MoveOutDate");

2 comments:

Anonymous said...

Thanks for this, it looked like quite an imposing error until i found your post :-)

JasRaj Bishnoi said...

but crystal report don't support Nullable variable, any solution..