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

后端 未结 23 1285
旧时难觅i
旧时难觅i 2020-11-28 20:19

I have the following code in my HomeController:

public ActionResult Edit(int id)
{
    var ArticleToEdit = (from m in _db.ArticleSet where m.storyId == id se         


        
相关标签:
23条回答
  • 2020-11-28 20:49

    The issue is that you're using ApplyPropertyChanges with a model object that has only been populated with data in the form (headline, story, and image). ApplyPropertyChanges applies changes to all properties of the object, including your uninitialized DateTime, which is set to 0001-01-01, which is outside of the range of SQL Server's DATETIME.

    Rather than using ApplyPropertyChanges, I'd suggest retrieving the object being modified, change the specific fields your form edits, then saving the object with those modifications; that way, only changed fields are modified. Alternately, you can place hidden inputs in your page with the other fields populated, but that wouldn't be very friendly with concurrent edits.

    Update:

    Here's an untested sample of just updating some fields of your object (this is assuming you're using LINQ to SQL):

    var story = _db.ArticleSet.First(a => a.storyId == ArticleToEdit.storyId);
    story.headline = ArticleToEdit.headline;
    story.story = ArticleToEdit.story;
    story.image = ArticleToEdit.image;
    story.modifiedDate = DateTime.Now;
    _db.SubmitChanges();
    
    0 讨论(0)
  • 2020-11-28 20:50

    [Solved] In Entity Framework Code First (my case) just changing DateTime to DateTime? solve my problem.

    /*from*/ public DateTime SubmitDate { get; set; }
    /*to  */ public DateTime? SubmitDate { get; set; }
    
    0 讨论(0)
  • 2020-11-28 20:50

    Got this problem when created my classes from Database First approach. Solved in using simply Convert.DateTime(dateCausingProblem) In fact, always try to convert values before passing, It saves you from unexpected values.

    0 讨论(0)
  • In my case, in the initializer from the class I was using in the database's table, I wasn't setting any default value to my DateTime property, therefore resulting in the problem explained in @Andrew Orsich' answer. So I just made the property nullable. Or I could also have given it DateTime.Now in the constructor. Hope it helps someone.

    0 讨论(0)
  • 2020-11-28 20:57

    I got this error after I changed my model (code first) as follows:

    public DateTime? DateCreated
    

    to

    public DateTime DateCreated
    

    Present rows with null-value in DateCreated caused this error. So I had to use SQL UPDATE Statement manually for initializing the field with a standard value.

    Another solution could be a specifying of the default value for the filed.

    0 讨论(0)
  • 2020-11-28 20:57

    I think the most logical answer in this regard is to set the system clock to the relevant feature.

     [HttpPost]
            public ActionResult Yeni(tblKategori kategori)
            {
                kategori.CREATEDDATE = DateTime.Now;
                var ctx = new MvcDbStokEntities();
                ctx.tblKategori.Add(kategori);
                ctx.SaveChanges();
                return RedirectToAction("Index");//listele sayfasına yönlendir.
            }
    
    0 讨论(0)
提交回复
热议问题