When converting a datetime2 data type to a datetime data type, an out-of-range value is returned.

c# datetime entity-framework orm sql-server

Question

I've got a datatable with 5 columns, where a row is being filled with data then saved to the database via a transaction.

While saving, an error is returned:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

It implies, as read, that my datatable has a type of DateTime2 and my database a DateTime; that is wrong.

The date column is set to a DateTime like this:

new DataColumn("myDate", Type.GetType("System.DateTime"))

Question

Can this be solved in code or does something have to be changed on a database level?

1
369
1/5/2018 3:01:14 PM

Accepted Answer

What kind of dates do you have in the column?

Do all of them fit within the range of the type?


As an aside, the correct way to get a Type object for the DataColumn constructor is the typeof keyword, which is orders of magnitude faster.

Therefore, to create the column, you should write

new DataColumn("myDate", typeof(DateTime))
60
8/26/2009 12:55:05 AM

Popular Answer

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!



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow