Link to home
Start Free TrialLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM

When I attempt to write a record to the database I get this error message.  I understand the error message occurs because I have left one of the date fields blank but in many cases the data is not necessary.  What is the easist way to program around this so this error does not occur if the field is left blank?

Here is some sample code where I get one of the data fields:

cmd.Parameters.Add("@DATEOFEXAM", SqlDbType.DateTime).Value = dtDateOfExam.Value;

cmd.ExecuteNonQuery();  --- error occurs here when the record is written to the table
Avatar of cuziyq
cuziyq

Assign the date field a default value in the table.  That way, if you attempt to insert a record into the table without specifying that field, it will get the default date you specify, and you will not receive an error.
Avatar of Pratima
Set allow null in Database to this field

and check
 Nullable<DateTime> dt = null;
if ( Trim(dtDateOfExam.Value) != "")
{
cmd.Parameters.Add("@DATEOFEXAM", SqlDbType.DateTime).Value = dtDateOfExam.Value;
}
else
{
 cmd.Parameters.Add("@DATEOFEXAM", SqlDbType.DateTime).Value = dt;
}
Avatar of kwh3856

ASKER

pratima,
Thanks for the quick reply.  I tried the code but the Trim command gives an error message

The name 'Trim' does not exist in the current context

I tried retyping the command and it did not show up as an available command

Am I missing something?

Thanks
Kenny
ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India 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
Avatar of kwh3856

ASKER

Top one worked.  Thank you very much.

Thanks
Kenny