Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
public string UpdateStudent(tbl_Student objStudenttbl, tbl_Address objAddress,tbl_BranchAcademic objBranchAcademic)
      {

          context = new TMSEntities();

          using (TransactionScope myTran = new TransactionScope())
          {
              try
              {
          var currentacadyearID = (from acdYear in context.tbl_AcademicYear
          where acdYear.IsDefault == 1 &&acdYear.status.Equals("A")
          select acdYear.AcademicYearID).FirstOrDefault();
                  int cid = Convert.ToInt32(currentacadyearID);
                  int fid = Convert.ToInt32(objBranchAcademic.FinancialYearID);

                      var Stud = (from stud in context.tbl_Student
                                  where stud.StudentID == objStudenttbl.StudentID
                                  select stud).FirstOrDefault();
                      if (Stud != null)
                      {
                          Stud.Title = objStudenttbl.Title;
                          Stud.Firstname = objStudenttbl.Firstname;
                          Stud.Middlename = objStudenttbl.Middlename;
                          Stud.LastName = objStudenttbl.LastName;
                          Stud.MaritalStatus = objStudenttbl.MaritalStatus;
                          Stud.Gender = objStudenttbl.Gender;
                          Stud.AdmissionDate = objStudenttbl.AdmissionDate;
                          Stud.FYID = objStudenttbl.FYID;
                          Stud.UpdateBy = objStudenttbl.UpdateBy;
                          Stud.UpdateDateTime = objStudenttbl.UpdateDateTime;
                          Stud.DOB = objStudenttbl.DOB;
                          Stud.SEmail = objStudenttbl.SEmail;


                          var Add = (from add in context.tbl_Address
                                     where add.AddressID == Stud.AddressID
                                     select add).FirstOrDefault();
                          Add.Address1 = objAddress.Address1;
                          Add.Address2 = objAddress.Address2;
                          Add.CityID = objAddress.CityID;
                          Add.StateID = objAddress.StateID;
                          Add.ContactID = objAddress.CountryID;
                          Add.Landmark = objAddress.Landmark;
                          Add.Postalcode = objAddress.Postalcode;
                          Add.Street = objAddress.Street;



                      var BranchAcad = (from branchAcad in context.tbl_BranchAcademic
                                            where branchAcad.AcademicYearID == cid
                                            select branchAcad).FirstOrDefault();
                          BranchAcad.FinancialYearID = fid;
                          context.SaveChanges();
//context.ObjectStateManager.ChangeObjectState(BranchAcad, //System.Data.EntityState.Modified);
                          context.AcceptAllChanges();
                  }
                  saved = true;
              }
              catch (OptimisticConcurrencyException e)
              {

                  strMessage = "Data_update_error";
                  log.Error(e.Message);
              }
              catch (Exception ex)
              {
                  strMessage = "Data_update_error";
                  log.Error(ex.Message);
              }

              finally
              {
                  if (saved)
                  {
                      myTran.Complete();
                      context.AcceptAllChanges();
                      strMessage = "Data_update_success";
                      context.Dispose();
                  }
              }
          }
          return strMessage;
      }

I am using above code and error is thrown after context.SaveChanges() satatement.
I am using MySQL db.I throws OptimisticConcurrencyException.I have also added statement in code to resolve this problem. But problem is still there. Plz suggest any solution.Thanks in advance..
Posted
Updated 19-Mar-15 18:51pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900