Edward.K
Edward.K

Reputation: 566

Store update, insert, or delete statement affected an unexpected number of rows (0).

i'm quite new to MVC. I was retrieved the value from database and show it in view, and i want to made changes and updated existing value. There is no problem on retrieved value, but when i create save, it returns the error at the db.changes line:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

I have try everything which is include the @Html.HiddenFor(m => m.Question_ID) in the Edit.cshtml but still no luck.

My CreateQuestionViewModel:

public class CreateQuestionViewModel
    {
        [HiddenInput(DisplayValue = false)]
        public int Question_ID { get; set; }

        [HiddenInput(DisplayValue=false)]
        public int Survey_ID { get; set; }

        [DisplayName("Question Title")]
        public string Qext_Text { get; set; }

        [DisplayName("Question Type")]
        public string Question_Type { get; set; }

        public bool Mandatory { get; set; }

        public string Language { get; set; }
    }
}

My Edit Controller:

  [HttpGet]
            public ActionResult Edit(int Question_ID = 0)
            {
                var viewModel = new CreateQuestionViewModel();
                viewModel.Question_ID = Question_ID;

                if (ModelState.IsValid)
                {
                    SURV_Question_Model surv_question_model = db.SURV_Question_Model.Find(Question_ID);
                    viewModel.Mandatory = surv_question_model.Question_Mandatory; 
                    viewModel.Question_Type = surv_question_model.Question_Type;

                    SURV_Question_Ext_Model surv_question_ext_model = db.SURV_Question_Ext_Model.FirstOrDefault(x => x.Qext_Question_ID.Equals(Question_ID));
                    viewModel.Qext_Text = surv_question_ext_model.Qext_Text;
                    viewModel.Language= surv_question_ext_model.Qext_Language;

                }
                return View(viewModel);
            }

        [HttpPost]
                public ActionResult Edit(CreateQuestionViewModel viewModel)
                {
                    if (ModelState.IsValid)
                    {
                        var question = new SURV_Question_Model();
                        question.Question_Mandatory = viewModel.Mandatory;
                        question.Question_Type = viewModel.Question_Type;

                        db.Entry(question).State = EntityState.Modified;
                        db.SaveChanges();

                        var question_ext = new SURV_Question_Ext_Model();
                        question_ext.Qext_Text = viewModel.Qext_Text;
                        question_ext.Qext_Language = viewModel.Language;

                        db.Entry(question_ext).State = EntityState.Modified;
                        db.SaveChanges();


                        return RedirectToAction("Edit", "SURV_Main", new { id = viewModel.Survey_ID });

                    }

Please give some guidance to me. Thanks!

Upvotes: 1

Views: 13272

Answers (1)

Sachu
Sachu

Reputation: 7766

You are creating a new question and question_ext and assigning value from the viewmodel passed. so if you try to save changes it will save as new row but before that you specify entitystate of them as modified so system can't modify.That why the error is coming. So first instead of creating a new create something like this and try

     var question = db.createviewquestionmodel.where
    (d=> d.question_id == viewmodel.question_id).firstordefault(); 

    question.Question_Mandatory = viewModel.Mandatory;
   question.Question_Type = viewModel.Question_Type;
   db.Entry(question).State = EntityState.Modified;
   db.SaveChanges();

then assign the values to question and try to save with edit mode

Upvotes: 5

Related Questions