public static Response SaveChanges(CenECommerceContext db) { try { db.SaveChanges(); return(new Response { Succeeded = true, }); } catch (Exception ex) { var response = new Response { Succeeded = false, }; if (ex.InnerException != null && ex.InnerException. InnerException != null && ex.InnerException. InnerException. Message.Contains("_Index")) { response.Message = "There is a record with the same value"; } else if (ex.InnerException != null && ex.InnerException. InnerException != null && ex.InnerException. InnerException. Message.Contains("REFERENCE")) { response.Message = "The record can't be delete because it has related records"; } else { response.Message = ex.Message; } return(response); } }
public static int GetPurchasesStatus(string description, CenECommerceContext db) { var purchaseStatus = db.PurchaseStatus. Where(ps => ps.Description == description). FirstOrDefault(); if (purchaseStatus == null) { purchaseStatus = new PurchaseStatus { Description = description, }; db.PurchaseStatus.Add(purchaseStatus); db.SaveChanges(); } return(purchaseStatus.PurchaseStatusId); }
public ActionResult Edit(User user) { if (ModelState.IsValid) { if (user.PhotoFile != null) { var folder = "~/Content/Photos"; var pic = string.Empty; var file = string.Format("{0}.jpg", user.UserID); var response0 = FilesHelpers. UploadPhoto( user.PhotoFile, folder, file); if (response0) { pic = string.Format("{0}/{1}", folder, user.UserID); user.Photo = pic; } } var db2 = new CenECommerceContext(); var currentUser = db2.Users.Find(user.UserID); if (currentUser.UserName != null) { UsersHelpers.UpdateUserName(currentUser.UserName, user.UserName); } db2.Dispose(); db.Entry(user).State = EntityState.Modified; var response = DBHelpers.SaveChanges(db); if (response.Succeeded) { return(RedirectToAction("Index")); } ModelState.AddModelError( string.Empty, response.Message); } ViewBag.CityId = new SelectList( ComboBoxHelpers. GetCities(user.StateId), "CityId", "NameCity", user.CityId); ViewBag.CompanyId = new SelectList( ComboBoxHelpers. GetCompanies(), "CompanyId", "NameCompany", user.CompanyId); ViewBag.StateId = new SelectList( ComboBoxHelpers. GetStates(), "StateId", "NameState", user.StateId); return(View(user)); }