private void AddStock(int amount) { using (var db = new DrinksModelContainer()) { List <Drinks> tempList = new List <Drinks>(); foreach (var row in selectedDrinks2) { tempList.Add(row); tester.ItemsSource = tempList.ToList(); } foreach (var row in tempList) { try { var query = from s in db.Drinks where s.Id == row.Id select s; foreach (Drinks drink in query) { if (drink.Stock != null) { drink.Stock = drink.Stock + amount; MessageBox.Show(string.Format("You have added {0} {1}", amount, drink.Name)); tbxStockQuantity.Text = null; selectedDrinks2.Clear(); } else { MessageBox.Show("There is no stock for this item"); } } // Submit the changes to the database. try { Console.WriteLine("Save Starting"); db.SaveChanges(); Console.WriteLine("Save Complete"); } catch (Exception e) { throw e; } } catch (Exception) { throw; } } } }
//updates the int property stock in the database private void UpdateStock() { using (var db = new DrinksModelContainer()) { List <Drinks> tempList = new List <Drinks>(); foreach (var row in selectedDrinks) { tempList.Add(row); } foreach (var row in tempList) { if (row.Stock != null) { var query = from s in db.Drinks where s.Id == row.Id select s; foreach (Drinks drink in query) { drink.Stock = drink.Stock - 1; } // Submit the changes to the database. try { Console.WriteLine("Save Starting"); db.SaveChanges(); Console.WriteLine("Save Complete"); } catch (Exception e) { Console.WriteLine(e); // Provide for exceptions. } } } } }