public void ChangePrice(string productNumber, decimal price) { // Modify the price of the selected product // If the update is successful then return true, otherwise return false. Product product = null; try { // Connect to the AdventureWorks database by using the Entity Framework using (AdventureWorksEntities database = new AdventureWorksEntities()) { if (!ProductExists(productNumber, database)) return; else { // Find the specified product product = (from p in database.Products where String.Compare(p.ProductNumber, productNumber) == 0 select p).First(); // Change the price for the product product.ListPrice = price; // Save the change back to the database database.SaveChanges(); } } } catch { // If an exception occurs, return false to indicate failure return; } // Notify the client that the price has been changed successfully ProductData productData = new ProductData() { ProductNumber = product.ProductNumber, Name = product.Name, ListPrice = product.ListPrice, Color = product.Color }; raisePriceChangedEvent(productData); }
public bool ChangeStockLevel(string productNumber, short newStockLevel, string shelf, int bin) { // Modify the current stock level of the selected product // in the ProductInventory table. // If the update is successful then return true, otherwise return false. // The Product and ProductInventory tables are joined over the // ProductID column. try { // Connect to the AdventureWorks database by using the Entity Framework using (AdventureWorksEntities database = new AdventureWorksEntities()) { // Find the ProductID for the specified product int productID = (from p in database.Products where String.Compare(p.ProductNumber, productNumber) == 0 select p.ProductID).First(); // Find the ProductInventory object that matches the parameters passed // in to the operation ProductInventory productInventory = database.ProductInventories.First( pi => String.Compare(pi.Shelf, shelf) == 0 && pi.Bin == bin && pi.ProductID == productID); // Update the stock level for the ProductInventory object productInventory.Quantity += newStockLevel; // Save the change back to the database database.SaveChanges(); } } catch { // If an exception occurs, return false to indicate failure return false; } // Return true to indicate success return true; }