// if the user confirm the changes private void btnConfirm_Click(object sender, EventArgs e) { // check if the new shipped date is valid (in range) or it is null if (IsValidData(DTPShippedDate) || (DTPShippedDate.Value == null)) { // creating a new order and setting its properties Order newOrder = new Order(); GetNewOrder(newOrder); // if the user wants to delete the shipped date if (delete == true) { newOrder.ShippedDate = null; } else { // the new order has a different shipped date newOrder.ShippedDate = Convert.ToDateTime(DTPShippedDate.Value); } // try to update data in the database try { if (!OrderDB.UpdateOrder(newOrder, order)) { MessageBox.Show("Another user has updated or " + "deleted that customer.", "Database Error"); this.DialogResult = DialogResult.Retry; } else // successfully updated { order.ShippedDate = newOrder.ShippedDate; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
/// <summary> /// Save changes to the shipping date for this order /// </summary> private void btnSave_Click(object sender, EventArgs e) { // first, have to check if the date is null or invalid (treat as null) before trying to parse it DateTime newDate; // somewhere to put the result bool parseResult = DateTime.TryParse(txtShippedDate.Text, out newDate); if (parseResult) // if the parse was successful (it's a legitimate date), compare against the other dates to make sure it's between them { if ((CurrentOrder.OrderDate != null && CurrentOrder.OrderDate > newDate) || (CurrentOrder.RequiredDate != null && CurrentOrder.RequiredDate < newDate)) { MessageBox.Show("Shipping Date must be later than order date and earlier than Required date"); // notify the user txtShippedDate.Text = ""; // clear out the bad date and focus the field for correction txtShippedDate.Focus(); return; // break out of this method entirely so they can fix it } } try { if (parseResult) // if the parse worked (and it was in the correct range), send the new date { OrderDB.UpdateOrder(CurrentOrder, newDate); } else // if it's blank or isn't a date { OrderDB.UpdateOrder(CurrentOrder, null); } // if we're still here and nothing messed up, close this and go back to the order list DialogResult = DialogResult.OK; this.Close(); } catch (Exception ex) { MessageBox.Show("Error saving shipping date: " + ex.Message, ex.GetType().ToString()); } }