Search code examples
c#sql-serverdatetimesqldatetime

Why can i insert MaxDate but not MinDate


when inserting into my table with a nullable datetime column, inserting DateTime.MinDate raises the error:

"The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."

Yet when i do MaxDate it works fine? I actually want to insert this value as null but in PropertyInfo.SetValue() passing null value is just automatically setting as MinDate, any suggestions?


Solution

  • Sql Server DateTime has a range of 1753-01-01 through 9999-12-31, while .NET's DateTime from 0001-01-01 12:00:00 midnight to 9999-12-31 11:59:59 P.M.

    So DateTime.MinValue is lower then the Sql Server's minimal value, while DateTime.MaxValue fits into the Sql Servers DateTime.


    Use nullables: DateTime? to be able to have a null in memory.