private void OrderForm_FormClosing(object sender, FormClosingEventArgs e) // If a user exits from creating order then delete order { if (MessageBox.Show("Are you sure you want cancel the order?", "Canceling Order", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (closedByBack == true) { CreateOrderForm createOrderForm = new CreateOrderForm(customerController); createOrderForm.Show(); } if (orderInProgress == true) { Collection <OrderItems> deletingItems = orderItemsController.FindByOrderID(order.OrderID); // putting all products to be deleted in a collection foreach (OrderItems eachItem in deletingItems) // iterating through items and adding them { Product eachProduct = productController.Find(eachItem.ProductID); // getting the product object eachProduct.QuantityInStock += eachItem.Quantity; orderItemsController.DataMaintenance(eachItem, DatabaseLayer.DB.DBOperation.Delete); // deleting each item on the order orderItemsController.FinalizeChanges(changingItem); productController.DataMaintenance(eachProduct, DatabaseLayer.DB.DBOperation.Edit); // reversing item in stock productController.FinalizeChanges(product); } orderController.DataMaintenance(order, DatabaseLayer.DB.DBOperation.Delete); // Now deleting the order orderItemsController.FinalizeChanges(changingItem); } } else { e.Cancel = true; this.Activate(); } }