Gerbrand Gerbrand - 11 months ago 105
C# Question

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

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
and my database a
; that is wrong.

The date column is set to a
like this:

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


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

Answer Source

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))