views:

41

answers:

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?

+1  A: 

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?

Andrey
Not that arnt nullable or are defaulted (DateEntered is set to getDate())
DazManCat
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
Andrey
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.
DazManCat
Ahhhh I didnt know that Andrey. Thanks!
DazManCat