Groups | Blog | Home
all groups > dotnet ado.net > january 2005 >

dotnet ado.net : DateTime fields and DBNull.Value


Wilker Shane Bruce
1/31/2005 12:37:05 PM
I have a stored procedure that has a couple of DateTime parameters that are
used to update nullable DateTime columns in one of my tables. When I call
the stored procedure using ADO.NET in C# from an OdbcCommand object and
attempt to set the DateTime parameters to null, I am getting the following
exception message: "String was not recognized as a valid DateTime."

The calling code for the parameters in question has the following form:

parm = command.Parameters.Add("@myDate", OdbcType.DateTime);
parm.Direction = ParameterDirection.Input;
parm.Value = DBNull.Value;

The exception occurs when executing command.ExecuteNonQuery(), A display of
the exception stacktrace shows that the exception is thrown within the Parse
method of the System.DateTimeParse class before a call is made to the stored
procedure. Can anyone give me some guidance as to why the system is trying
to perform a parse on a field that has been set to the DBNull.Value?

Thanks in advance for any assistance you can provide.

Vico
5/4/2005 10:34:03 AM
You may want to take a look on this link
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdbnullclassvaluetopic.asp

Specially on the paragraph:
"Data intensive applications accessing SQL databases must use the
System.Data.SqlTypes classes, which have inherent support for null values."

[quoted text, click to view]
AddThis Social Bookmark Button