public void Save() { PTCData db = null; Messages.Clear(); ResetException(); try { db = new PTCData(); // Ensure the correct category is set if (Entity.CategoryId == null) { Entity.Category = db.Categories.Find(Entity.Category.CategoryId); } else { Entity.Category = db.Categories.Find(Entity.CategoryId); } Entity.CategoryId = Entity.Category.CategoryId; // Either Update or Insert product if (PageMode == PDSAPageModeEnum.Edit) { db.Entry(Entity).State = EntityState.Modified; db.SaveChanges(); } else if (PageMode == PDSAPageModeEnum.Add) { db.Products.Add(Entity); db.SaveChanges(); } // Get all the data again in case anything changed Get(); } catch (DbEntityValidationException ex) { IsValid = false; // Validation errors foreach (var errors in ex.EntityValidationErrors) { foreach (var item in errors.ValidationErrors) { Messages.AddModelError(item.PropertyName, item.ErrorMessage); } } // Set page state SetUIState(PageMode); } catch (Exception ex) { LastException = ex; Message = "ERROR Saving Product"; } }
public virtual void Delete() { PTCData db = null; try { db = new PTCData(); if (!string.IsNullOrEmpty(EventArgument)) { Entity = db.Products.Find(Convert.ToInt32(EventArgument)); db.Products.Remove(Entity); db.SaveChanges(); PageMode = PDSAPageModeEnum.List; } } catch (Exception ex) { Publish(ex, "Error Deleting Product With ID=" + Entity.ProductName); } }
public void Delete(int productId) { PTCData db = null; ResetException(); try { db = new PTCData(); Product product = db.Products.Find(productId); // Attempt to delete db.Products.Remove(product); db.SaveChanges(); // Reload all Products Get(); } catch (Exception ex) { LastException = ex; Message = "ERROR Deleting Product with ID: " + productId.ToString(); } }
protected void Update() { PTCData db = null; try { db = new PTCData(); // Do editing here db.Entry(Entity).State = EntityState.Modified; db.SaveChanges(); PageMode = PDSAPageModeEnum.List; } catch (DbEntityValidationException ex) { IsValid = false; ValidationErrorsToMessages(ex); } catch (Exception ex) { Publish(ex, "Error Updating Product With ID=" + Entity.ProductId.ToString()); } }
protected void Insert() { PTCData db = null; try { db = new PTCData(); // Do editing here db.Products.Add(Entity); db.SaveChanges(); PageMode = PDSAPageModeEnum.List; } catch (DbEntityValidationException ex) { IsValid = false; ValidationErrorsToMessages(ex); } catch (Exception ex) { Publish(ex, "Error Inserting New Product: '" + Entity.ProductName + "'"); } }