3

I have a piece of code that tries to insert a date into a database using linq to sql.

 rec.CreationDateTime =System.DateTime.Now;

This line throws the exception

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

I have read the different solutions suggested online. What i don't understand is how System.DateTime.Now can be outside the range that would cause this exception.

System.DateTime.Now is certainly far above anywhere near "1753". Likewise the year "9999" is quite some way ahead of 2015.

Can someone enlighten me on how to solve this problem?

6
  • 1
    Do you have any other DateTime field in your table? Commented Feb 22, 2015 at 7:14
  • no. Why would that matter? Commented Feb 22, 2015 at 7:18
  • 4
    default value for DateTime in .Net is DateTime.MinValue which is 01/01/0001. If there was another DateTime column in your table, it might not have been set a value from your code, so it automatically gets DateTime.MinValue as its value which results in the same exception Commented Feb 22, 2015 at 7:21
  • 3
    It would seem that there is some DateTime column that you're trying to insert NULL into (or e.g. the result of an empty string being converted to a DateTime). FInd that DateTime, fix it to have a proper value for SQL Server - done. Or change the SQL Server datatype to DATETIME2(3) to avoid these issues altogether (SQL Server 2008 and newer) Commented Feb 22, 2015 at 7:21
  • This makes a lot of sense. I will double check my code and come back. many thanks to both of you for your help Commented Feb 22, 2015 at 7:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.