1

Im getting a strange error when trying to add a new item to a table using LINQ

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

Code:

dbc.TblEvent newEvent = new dbc.TblEvent();
        newEvent.Subject = "Registered";
        newEvent.PersonID = PersonId;
        newEvent.EventDate = DateTime.Now;
        newEvent.EventTypeID = 23;
        newEvent.EventStatusID = 10;
        context.TblEvents.InsertOnSubmit(newEvent);

As you can see I'm not doing anything exciting. What can I do to sort this?

2 Answers 2

5

99 out of 100 times a date time overflow in linq happens because the date is not being set. DateTime has a default value that is lower than the lowest date sql can handle.

Check all your assignments. Are you missing another date time field in your table?

Sign up to request clarification or add additional context in comments.

3 Comments

Not that arnt nullable or are defaulted (DateEntered is set to getDate())
Default is the problem. Linq will not respect the default by default and will instead assign an min datetime. Set the AutoGenerated property for the field in the linq designer to true
Ahhh yes spotted the problem. You were right there was a date field missing, even though it has a default in the DB it still complained.
0

Another scenario for this: changed a query from a LEFT JOIN to an INNER JOIN, hence making a previously nullable datetime field, non-nullable. Needed to delete the table from the .dbml and re-add to get his to refresh the model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.