C # DateTime Default Issue 01/01/0001 00:00:00

I am having trouble injecting DateTime into the database with the following error:

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

      

I am aware that the SQL date must be between 1/1/1753 12:00:00 and 12/31/9999, however my date seems to remain at 01/01/0001 00:00:00.

I have the following date defined in a web service method:

  [DataMember]
  public DateTime RecordTimeStamp { get; set; }

      

This is used in the following code to add to the database

  sqlComm.Parameters.Add("@RecordTimeStamp", SqlDbType.DateTime).Value = pCustomer.RecordTimeStamp;

      

This code takes its value from the aspx page with the code

  DateTime now = DateTime.Now;
  pCustomer.RecordTimeStamp = now;

      

I'm trying to get this to insert the current date into the database, but it doesn't seem to change from the default.

+3


source to share


1 answer


Try to change the property

from

[DataMember]
public DateTime RecordTimeStamp { get; set; }

      



in

[DataMember]
public DateTime? RecordTimeStamp { get; set; }

      

Hope this solves the problem. What is happening is the DateTime NOT NULL datatype and it enforces the default there (01/01/0001) to make sure a non-empty date will be displayed. I don't know why it doesn't accept the changed value and throws an "out of range" exception. This is something you might want to explore further.

+3


source







All Articles