kwh3856
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("@DATEO FEXAM", SqlDbType.DateTime).Value = dtDateOfExam.Value;
cmd.ExecuteNonQuery(); --- error occurs here when the record is written to the table
Here is some sample code where I get one of the data fields:
cmd.Parameters.Add("@DATEO
cmd.ExecuteNonQuery(); --- error occurs here when the record is written to the table
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.
Set allow null in Database to this field
and check
Nullable<DateTime> dt = null;
if ( Trim(dtDateOfExam.Value) != "")
{
cmd.Parameters.Add("@DATEO FEXAM", SqlDbType.DateTime).Value = dtDateOfExam.Value;
}
else
{
cmd.Parameters.Add("@DATEO FEXAM", SqlDbType.DateTime).Value = dt;
}
and check
Nullable<DateTime> dt = null;
if ( Trim(dtDateOfExam.Value) != "")
{
cmd.Parameters.Add("@DATEO
}
else
{
cmd.Parameters.Add("@DATEO
}
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Top one worked. Thank you very much.
Thanks
Kenny
Thanks
Kenny