Gerbrand Gerbrand - 2 months ago 23
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
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?

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