Reputation: 193
I am having a weird problem inserting a date value into my database. When debugging my program, i realized it inserts the first five rows into the database but on the sixth it throws an exception. I verified if it was a syntax error or if something changed along the way by checking the first 5 queries that could execute successfully with the sixth and they are both the same. If there is a problem with the type, it should throw the exception from the beginning. What do you think?
This is the error I get
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
Upvotes: 2
Views: 508
Reputation: 5968
I think your problem comes because of the difference between DateTime type in C# which start at 01-01-0001 and in SQL Server which is `January 1, 1753, through December 31, 9999', check this link, so I think (due to the error Jon explained) you are trying to insert a Date value which is less than 01-01-1753 into SQL Server. That's why the SQL Exception is shown.
Upvotes: 0
Reputation: 1499860
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
That suggests you're specifying the value as a string, and performing a conversion. Don't do that. Avoid string conversions wherever you possibly can. (I suspect there's quite possibly a disconnect between the string format you're providing and the one that's being used for parsing. Removing the string conversion completely is the best fix for this.)
Instead, provide the value as a SqlParameter
with a DateTime
value.
Upvotes: 5