public string DeleteProduct(int id) { string result = ""; Products product = new Products(); using (NORTHWNDEntities context = new NORTHWNDEntities()) { try { product = context.Products.FirstOrDefault(p => p.ProductID == id); context.Products.Remove(product); context.SaveChanges(); result = "The record has deleted succesfully."; } catch (Exception ex) { result = "An error occurred during this operation."; } } return result; }
public string InsertProduct(Products product) { string result = ""; using (NORTHWNDEntities ent = new NORTHWNDEntities()) { try { ent.Products.Add(product); ent.SaveChanges(); result = "The product has added successfully"; } catch (Exception) { result = "An error occurred during this operation."; } } return result; }
public string UpdateProduct(Products product, int id) { string result = ""; using (NORTHWNDEntities context = new NORTHWNDEntities()) { Products currentproduct = new Products(); try { currentproduct = context.Products.FirstOrDefault(p => p.ProductID == id); currentproduct.ProductName = product.ProductName; currentproduct.CategoryID = product.CategoryID; currentproduct.SupplierID = product.SupplierID; currentproduct.QuantityPerUnit = product.QuantityPerUnit; currentproduct.UnitPrice = product.UnitPrice; currentproduct.UnitsInStock = product.UnitsInStock; currentproduct.UnitsOnOrder = product.UnitsOnOrder; currentproduct.ReorderLevel = product.ReorderLevel; currentproduct.Discontinued = product.Discontinued; context.SaveChanges(); result = "The record has updated succesfully."; } catch (Exception ex) { result = "An error occurred during this operation."; } } return result; }