How to fix "SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value."
asp.net, c#, entity-framework, webforms
Solution
The root of your problem is that the C# DateTime object is "bigger" than SQL's smalldatetime type. Here's a good overview of the differences: http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
So really your options are:
- Change the column type from smalldatetime to datetime (or datetime2)
- Instead of using EF, construct your own SQL Command (and you can use SqlDateTime)
Problem
SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. my code is like this: ``` using (var contxt = new realtydbEntities()) { var status = GetStatus(); var repIssue = new RepairIssue() { CreaterId = AuthorId, RepairItemDesc = this.txtDescription.Text, CreateDate = DateTime.Now,//here's the problem RepairIssueStatu = status }; contxt.AddObject("RepairIssues", repIssue); contxt.SaveChanges(); } ``` the CreateDate property mapping to a column which type is smalldatetime. how to make this code run?