When converting a datetime2 data type to a datetime data type, an out-of-range value was returned.

asp.net-mvc datetime entity-framework

Question

I am modifying an existing database in an ASP.NET MVC application to change a specific field.DateTime . Two of the four fields in my database are DateCreated and DateModified. There is no need to modify the field's creation date when I attempt to update it, thus I want to retain the DateCreated time the same and change the DateModified time to the present time usingDateTime.Now

Just in case I'm doing anything incorrectly, here is the provided code. Please be patient with me while I become used to utilizing ASP.NET MVC. I've seen various solutions whereContext is named, but I can't locate any mention of it elsewhere. I get the error message mentioned in the application's title and thecontractEntity.SaveChanges() shown 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");
}

I would much appreciate your assistance. Thanks.

1
22
12/27/2010 3:30:12 AM

Accepted Answer

I discovered to open the.edmx file for my database and change: after reading these pages

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

to

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

Is this acceptable or is there a more effective way to correct that mistake?

19
11/8/2010 11:35:27 PM

Popular Answer

I had the similar problem, except mine was caused by the fact that an erroneous DateTime was automatically generated when I saved my Model. The value of a column called CreatedOn that I had was 01/01/0001, which was an incorrect DateTime for SQL since I hadn't changed it to anything. Setting it resolved the issue for me.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow