So you have T-SQL insert statement for a table which has one of its columns defined as DATETIME which allows NULLs. You are passing an uninitialized variable for exactly this column in your INSERT (not purposely of course :) ), and the error you get is:
System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between
1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
What do you do? Well, I was very confused seeing this message. I hit the Internet for a similar problem but I was totally deluded in stories that SqlDataType is not compatible with .NET DataTime type. Obviously I was on a wrong direction. Thus, after I reviewed my code, I saw that the value of the DateTime column, which allows NULLs is not actually NULL but rather DateTime.MinValue - indeed, and this is normal because each .NET variable once defined it is initialized with some default value (which in the DateTime case is DateTime.MinValue). Unfortunately, DateTime.MinValue doesn't really fit into the range of possible values for SqlDataTime.
What is the resolution? Well, I am using .NET 2.0, where there is a very neat generic type named Nullable
I was getting something similar using a GridView. I was passing two DateTime parameters. I tried your solution but that didn't solve the problem. I then set the ObjectDataSource SelectParameters programmatically:
ReplyDeletesourceEvents.SelectParameters.Add("startDate", startDate.ToShortDateString());
sourceEvents.SelectParameters.Add("endDate", endDate.ToShortDateString());
It worked!