//called to refresh all the fields and datagridview private void populateFields(Button disableButton, Button enableButton, int index) { orders = OrderDB.GetOrders(); txtOrderID.Text = orders[index].OrderID.ToString(); txtCustomerID.Text = orders[index].CustomerID; dtpOrderDate.Value = (DateTime)orders[index].OrderDate; dtpRequiredDate.Value = (DateTime)orders[index].RequiredDate; if (orders[index].ShippedDate != null) // if the shippeddate is not null { dtpShippedDate.Format = DateTimePickerFormat.Long; dtpShippedDate.Value = (DateTime)orders[index].ShippedDate; isNull = false; } else //shipped date is null { dtpShippedDate.CustomFormat = " "; dtpShippedDate.Format = DateTimePickerFormat.Custom; isNull = true; } List <OrderDetails> orderDetails_List = OrderDetailsDB.GetOrdersDetails(orders[index].OrderID); dgvOrderDetails.DataSource = orderDetails_List; txtTotalCharges.Text = CalculateCharges(orderDetails_List).ToString("c"); Disablebuttion(disableButton, enableButton, index); }
// need to retrieve the list of OrderIDs upon form loading private void Form1_Load(object sender, EventArgs e) { try { orders = OrderDB.GetOrders(); // get the list of OrderIDs foreach (var order in orders) // add items to the OrderID combobox { orderIDComboBox.Items.Add(order.OrderID); } } catch (Exception ex) { MessageBox.Show("Error while loading order data: " + ex.Message, ex.GetType().ToString()); } }
// ----------------- Methods ------------------ // Do Updates Against Database private void TryUpdate() { // 1. pass listToUpdate to OrderDB update method // 2.1 if thrown, show exception message // 2.2 if no exception, return a List<int> (collection of update failed OrderIDs) // 3. show user how many updates succeed, how many failed, and OrderID of failed ones // 4.1 if all updates succeed, clear listToUpdate, disable save btn (only enabled when there are unupdated changes) // 4.2 if there are failed updates, add failed orders to listToUpdate again, reload orders table var failedOrderIDs = new List <int>(); try { // try to update changes failedOrderIDs = OrderDB.UpdateOrders(listToUpdate); // create error message shows which orders failed update string failMsg = "Order ID: "; foreach (var failedID in failedOrderIDs) { failMsg += failedID + ", "; } failMsg += "did not update due to concurrency issue, please try again later."; // use messagebox to give user feedback int howManySucceed = listToUpdate.Count - failedOrderIDs.Count; MessageBox.Show((howManySucceed == 0 ? "" : howManySucceed.ToString() + " order(s) updated.\n") + (failedOrderIDs.Count == 0 ? "" : failedOrderIDs.Count + " order(s) failed to update. " + failMsg)); // if there are failed updates, add them to listToUpdate again, fetch newest data from database, so user can try to update again if (failedOrderIDs.Count > 0) { var updateFailedOrders = new List <Order>(); foreach (var failedOrderID in failedOrderIDs) { foreach (var order in listToUpdate) { if (order.OrderID == failedOrderID) { updateFailedOrders.Add(order); } } } listToUpdate = updateFailedOrders; // fetch newest data, bind to orders, so that data on form is up to date allOrders = OrderDB.GetOrders(); orderBindingSource.DataSource = allOrders; // if current order is not in update list, hide date change indicators if (!listToUpdate.Contains(currentOrder)) { grpDateChangeVisualIndicator.Visible = false; lblDateChangeTextIndicator.Visible = false; } } else // if all updated successfully, clear update list, disable save { listToUpdate.Clear(); // save button is only enabled when there are unsaved changes in listToUpdate orderBindingNavigatorSaveItem.Enabled = false; grpDateChangeVisualIndicator.Visible = false; lblDateChangeTextIndicator.Visible = false; } } catch (Exception ex) { // if exception is thrown, show error message MessageBox.Show("Updates failed due to " + ex.Message, ex.GetType().ToString()); } }