title: Why is my code not able to update the database?
body: <p>I am having trouble saving my entities after updating them. I can add new entities like this:  add(student); but if I tried this:</p>
  
  <pre><code>if (ModelState.IsValid)
  {
     db.Entry(student).State = EntityState.Modified;
     db.SaveChanges();
     return RedirectToAction("someView");
  }
  </code></pre>
  
  <p>I get this error message:</p>
  
  <blockquote>
    <p>System.Data.Entity.Infrastructure.DbUpdateConcurrencyException was unhandled by user code<br>
    Message=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.</p>
  </blockquote>
  
  <p>Here’s my controller method:</p>
  
  <pre><code>[HttpPost]
  public ActionResult ClassAttendance(InstructorIndexData viewModel, FormCollection frmcol)
  {
       var instructorData = new InstructorIndexData();
  
       string[] AllFstMNames = frmcol["item.Student.FirstMidName"].Split(',');
       string[] AllLstNames = frmcol["item.Student.LastName"].Split(',');
       string[] AllAddresses = frmcol["item.Student.Address"].Split(',');
       string[] AllEnrollmentDates = frmcol["item.Student.EnrollmentDate"].Split(',');
       //more of the same code…
  
       var student = new Student();
       var enrollment = new Enrollment();
  
       for ( int i = 0; i &lt; AllFstMNames.Count(); i++)
       {
           student.FirstMidName = AllFstMNames[i];
           student.LastName = AllLstNames[i];
           student.Address = AllAddresses[i];
           student.EnrollmentDate = Convert.ToDateTime(AllEnrollmentDates[i]);
  
           if (!string.IsNullOrEmpty(frmcol["item.Grade"]))
           {
              enrollment.Grade = Convert.ToInt32(AllGrades[i]);
           }
  
           enrollment.StudentID = Convert.ToInt32(AllStudentIds[i]);
           enrollment.attendanceCode = Convert.ToInt32(AllAttendanceCodes[i]);
           enrollment.classDays = AllclassDays[i];
           enrollment.CourseID = Convert.ToInt32 (AllCourseIds[i]);
           //update rows
        }
  
        if (ModelState.IsValid)
        {
           db.Entry(student).State = EntityState.Modified;
           db.SaveChanges();
           return RedirectToAction("someView");
        }
  </code></pre>
  
  <p>Can you help me with just being able to update values in the database?</p>