public ActionResult AddNewProduct(Product p) { if (SessionCheck.checkSession(Session["name"] as string)) { HttpPostedFileBase file = Request.Files[0] as HttpPostedFileBase; dep.AddnewProduct(p, file); return RedirectToAction("AddNewProduct"); } else { return Redirect("Home/index"); } }
public void AddnewProduct(Product p, HttpPostedFileBase file) { using (DBcontextclasses cx = new DBcontextclasses()) { string filename = null; string[] str = file.FileName.Split('\\'); filename = str[str.Length - 1]; filename.Remove(0, 1); var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Content\\images\\", (filename)); p.path = "~/Content/images/" + filename; p.rating = 0; file.SaveAs(path); cx.Products.Add(p); try { // Your code... // Could also be before try if you know the exception occurs in SaveChanges cx.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Debug.Print("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Debug.Print("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } }
public void updateProduct(Product p) { using (DBcontextclasses cx = new DBcontextclasses()) { var x = cx.Products.Find(p.Id); x.name = p.name; x.type = p.type; x.price = p.price; try { // Your code... // Could also be before try if you know the exception occurs in SaveChanges cx.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Debug.Print("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Debug.Print("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } }
public ActionResult UpdateConfrim(Product p) { dep.updateProduct(p); return RedirectToAction("updateProducts"); }