Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

I have an ASP.NET MVC application where I am editing an existing database to update a paticular field, DateTime. My database has 4 fields, two of which are DateCreated and DateModified. When I try to update the field, I want to keep DateCreated time the same, no reason to update the date it was created, and I change the DateModified time to the current time using DateTime.Now

Here is the given code just in-case I am doing something wrong. This is my first time using ASP.NET MVC so go gentle. I have seen other answers where Context is called, but I can't find any reference to it. When I run the application I receive the error message in the title and the contractEntity.SaveChanges() is in red.

public ActionResult Edit(Contract editContract) {
var contract = (from c in contractEntity.Contracts where c.Id == editContract.Id select c).First();
if (!ModelState.IsValid)
    return View(contract);
// editContract.DateCreated = contract.DateCreated;
// editContract.DateModified = DateTime.Now;
  contractEntity.ApplyCurrentValues(contract.EntityKey.EntitySetName, editContract);
  contractEntity.SaveChanges();
  return RedirectToAction("Index");
}

Please, any help is appreciated. Thanks.

like image 547
Anthony Forloney Avatar asked Nov 05 '09 05:11

Anthony Forloney


People also ask

What is a DATETIME2 data type?

SQL DateTimeOffset. The DATETIME2 data type specifies a date and time with fractional seconds. DATETIME2 supports dates from 0001-01-01 through 9999-12-31. The default value is 1900-01-01 00:00:00. The time is based on a 24-hour clock.

What is the difference between DATETIME2 and datetime?

The main difference is the way of data storage: while in Datetime type, the date comes first and then time, in Datetime2, 3 bytes, in the end, represents date part!

Is DATETIME2 better than datetime?

Microsoft recommends using DateTime2 instead of DateTime as it is more portable and provides more seconds precision. Also, DateTime2 has a larger date range and optional user-defined seconds precision with higher accuracy.

What is DATETIME2 data type in SQL Server?

Defines a date that is combined with a time of day that is based on 24-hour clock. datetime2 can be considered as an extension of the existing datetime type that has a larger date range, a larger default fractional precision, and optional user-specified precision.


2 Answers

For me, I had the same issue, but the problem was that by default when I saved my Model, an invalid DateTime was being created. I had a field CreatedOn and had not set it to anything, which meant the value was 01/01/0001 which was an invalid DateTime for SQL. Setting it got rid of the problem for me.

like image 98
Colin Asquith Avatar answered Oct 14 '22 04:10

Colin Asquith


After reading this website I found to open the .edmx file for my database and change:

<...Provider="System.Data.SqlClient" ProviderManifestToken="2008".../>

to

<...Provider="System.Data.SqlClient" ProviderManifestToken="2005".../>

Is this acceptable or is there a better approach to fixing that error?

like image 42
Anthony Forloney Avatar answered Oct 14 '22 06:10

Anthony Forloney