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

cdatetimeentity-frameworkormsql server

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?

Best 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!