Here is a snippet of my model:
* SQL Server *
Event
-----
Id (int, PK) NOT NULL
Title (varchar(100)) NULL
LastModified (datetime) NULL
EventDates
----------
Id (int, PK) NOT NULL
StartDate (datetime) NULL
EndDate (datetime) NULL
* C# *
Event
-----
public int Id
public string Title
public DateTime LastModified
EventDates
----------
public int Id
public DateTime StartDate
public Datetime EndDate
The LastModified field has been in the database since its creation. I have been saving it’s value when I save an event, but I want to display it in a table, so I changed up my Event repository’s GetEvents‘s return value:
return (from e in GetDbEvents()
select new Event
{
// Miscellaneous fields..
LastModified = e.LastModified.GetValueOrDefault() // Shiny new code
});
When I call this now, I get yelled at:
The conversion of a char data type to a datetime data type
resulted in an out-of-range datetime value.
If I strip down the above code to this, it still doesn’t help and I get the same error if I attempt to enumerate over the result:
var test = (from e in _db.Events
select e.LastModified.GetValueOrDefault());
As a test, I did the same statement for my EventDates table (again, with 2 datetime columns):
var test4 = (from ed in _db.EventDates
select new EventDate
{
StartDate = ed.StartDate.GetValueOrDefault(),
EndDate = ed.EndDate.GetValueOrDefault()
});
This works fine, of course. No errors when I enumerate, all values are correct.
I should point out that some LastModified values in the Events table are NULL while all values in EventDates are populated with data.
Edit
My main question is why does Events give me out-of-range issues and EventDates does not, even though the model is quite similar?
The problem is with the GetValueOrDefault(). This will return
DateTime.MinValue(01-01-0001) in the “default” case and sqlserver doesn’t accept that (it refuses dates before about 1753).If you use a
Nullable<DateTime>, then sql will be happy with thenullit will get.