// Purchase Stock Processing Button Event private void btnPurchase_Click(object sender, EventArgs e) { try { int purchaseCnt = int.Parse(tbPurchaseCnt.Text); if (selectedProductId >= 0) { context.Products.Load(); // Find product instance which was clicked var result = context.Products.SingleOrDefault(p => p.productId == selectedProductId); if (result != null) { result.quantityAvailable += purchaseCnt; // Add number of purchasing context.SaveChanges(); showStocks(); tbPurchaseCnt.Text = ""; lbProductId.Text = ""; selectedProductId = -1; MessageBox.Show("Purchasing " + purchaseCnt + " of " + result.productName + " processed successfully"); } } else { MessageBox.Show("Please click product to purchase on the list"); } }catch (Exception ex) { MessageBox.Show("Please input correct purchase count: " + ex.Message); } }
//Function to add a new order from cart private void buttonOrder_Click(object sender, EventArgs e) { if (productsToAdd.Count == 0) { MessageBox.Show("Please Add items to the cart first"); return; } foreach (orderedProduct x in productsToAdd) { context.Orders.Add(new Order { orderId = x.orderId, productId = x.product.productId, quantity = x.qty, delivered = 0, orderDate = DateTime.Today.ToString() }); } context.SaveChanges(); MessageBox.Show("Order Placed Successfuly"); productsToAdd.Clear(); dataGridViewCart.DataSource = productsToAdd.ToList(); }
// Clear all tables' rows and put initial mock data private void btnInit_Click(object sender, EventArgs e) { context.Orders.Load(); context.Categories.Load(); context.Customers.Load(); context.Products.Load(); context.Orders.Local.Clear(); context.Categories.Local.Clear(); context.Customers.Local.Clear(); context.Products.Local.Clear(); context.Customers.AddRange((new CutomerMockData()).getMockData()); context.SaveChanges(); context.Categories.AddRange((new CategoryMockData()).getMockData()); context.SaveChanges(); context.Products.AddRange((new ProductMockData()).getMockData()); context.SaveChanges(); MessageBox.Show("Database is successfully initialized"); }