c# – Conversion of a datetime2 data type to a datetime data type results out-of-range value

c# – Conversion of a datetime2 data type to a datetime data type results out-of-range value

This can happen if you do not assign a value to a DateTime field when the field does not accept NULL values.

That fixed it for me!

Both the DATETIME and DATETIME2 map to System.DateTime in .NET – you cannot really do a conversion, since its really the same .NET type.

See the MSDN doc page: http://msdn.microsoft.com/en-us/library/bb675168.aspx

There are two different values for the SqlDbType for these two – can you specify those in your DataColumn definition?

BUT: on SQL Server, the date range supported is quite different.

DATETIME supports 1753/1/1 to eternity (9999/12/31), while DATETIME2 supports 0001/1/1 through eternity.

So what you really need to do is check for the year of the date – if its before 1753, you need to change it to something AFTER 1753 in order for the DATETIME column in SQL Server to handle it.

Marc

c# – Conversion of a datetime2 data type to a datetime data type results out-of-range value

In my SQL Server 2008 database, I had a DateTime column flagged as not nullable, but with a GetDate() function as its default value. When inserting new object using EF4, I got this error because I wasnt passing a DateTime property on my object explicitly. I expected the SQL function to handle the date for me but it did not. My solution was to send the date value from code instead of relying on the database to generate it.

obj.DateProperty = DateTime.now; // C#

Leave a Reply

Your email address will not be published.