ZeeeeeV
ZeeeeeV

Reputation: 381

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

I am having trouble inserting a DateTime into a 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 AM and 12/31/9999, however my date seems to remain at the 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 into database

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

This code takes its value from an aspx page with code

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

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

Upvotes: 6

Views: 21721

Answers (1)

Balash
Balash

Reputation: 153

Try to change the property

from:

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

to:

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

Hopefully this will fix the issue. What happens is DateTime is NOT NULL data type and it enforces to put a default value there (01/01/0001) to make sure that non-null date will be submitted. I don't know why it does not accept altered value and throws out an "out of range" exception. That is something you might want to investigate further.

Upvotes: 6

Related Questions