2

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?

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
Grace
  • 2,548
  • 5
  • 26
  • 23
  • What programming language and database system are you using? – srgerg Mar 08 '11 at 11:02
  • i guess you are using reflection here as you do "PropertyInfo.SetValue()" but can you show us some more code, and what class are you in as you are referring to this in your comment below. – Peter Mar 08 '11 at 12:27

1 Answers1

3

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.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • Nice and efficient then :S any way of inserting a plain old null to this value via c# when running an insert? – Grace Mar 08 '11 at 11:12
  • Just make the field nullable on db and don't pass any values if null. – Pabuc Mar 08 '11 at 11:14
  • If you are working with SqlCommands directly use DBNull.Value instead of null! – Peter Mar 08 '11 at 11:22
  • I have tried not setting it at all and also tried PropertyInfo.SetValue(this, DBNull.Value, null) and both are still sending in MinValue to the command text... – Grace Mar 08 '11 at 11:26
  • 8
    You can also use `SqlDateTime.MinValue`. – Residuum Mar 08 '11 at 11:31