public ActionResult Index(Product product, HttpPostedFileBase FileUpload) { using (AlliedEntities db = new AlliedEntities()) { try { if (ModelState.IsValid) { ViewBag.Msg = null; if (FileUpload != null) { if (FileUpload.ContentLength <= (512 * 512) && (FileUpload.ContentType.Contains("/jpg") || FileUpload.ContentType.Contains("/jpeg") || FileUpload.ContentType.Contains("/png") || FileUpload.ContentType.Contains("/bmp"))) { product.imagetype = FileUpload.ContentType; product.imagebytes = ConverToByte(FileUpload); ViewBag.Msg = null; } else { ViewBag.Msg = "Invalid File"; } } if (ViewBag.Msg == null) { db.Products.Attach(product); db.ObjectStateManager.ChangeObjectState(product, System.Data.EntityState.Modified); db.SaveChanges(); return RedirectToAction("Index", "Manage"); } else { var q = (from p in db.Products where p.id == product.id select p).FirstOrDefault(); ViewBag.Msg = "Something went wrong"; return View(q); } } else { var q = (from p in db.Products where p.id == product.id select p).FirstOrDefault(); ViewBag.Msg = "Something went wrong"; return View(q); } } catch (Exception e) { var q = (from p in db.Products where p.id == product.id select p).FirstOrDefault(); ViewBag.Msg = "Something went wrong"; return View(q); } } }
public ActionResult Delete(int id) { if (Session["user"] != null) { using (AlliedEntities db = new AlliedEntities()) { Product p = db.Products.Single(e => e.id == id); db.Products.DeleteObject(p); db.SaveChanges(); return RedirectToAction("Index", "Manage"); } } else { return RedirectToAction("Index", "Home"); } }
public ActionResult Index(Product item, HttpPostedFileBase FileUpload) { if (ModelState.IsValid){ try{ using(AlliedEntities db = new AlliedEntities()){ if (FileUpload != null) { if (FileUpload.ContentLength <= (512 * 512) && (FileUpload.ContentType.Contains("/jpg") || FileUpload.ContentType.Contains("/jpeg") || FileUpload.ContentType.Contains("/png") || FileUpload.ContentType.Contains("/bmp"))) { item.imagetype = FileUpload.ContentType; item.imagebytes = ConverToByte(FileUpload); ViewBag.Msg = null; } else { ViewBag.Msg = "Invalid File"; } } if (ViewBag.Msg == null) { item.dateadded = DateTime.Now.Date; db.Products.AddObject(item); db.SaveChanges(); ViewBag.Msg = "Successfully Added"; return View(); } else { return View(); } } } catch (Exception) { return View(); } } return View(); }