/// <summary> /// Deletes customer from database using the supplied customer ID. /// If the customer exists, they are deleted from the database. If the customer /// doesn't exist an exception is thrown. /// </summary> private void btnDelete_Click(object sender, RoutedEventArgs e) { try { if (String.IsNullOrWhiteSpace(txtCustomerID.Text)) { throw new ArgumentNullException("No customer ID provided."); } // Check to see if the customer exists in the store Customer customer = store.Find(int.Parse(txtCustomerID.Text)); // Throw new exception if customer doesn't exist, otherwise delete the customer if (customer == null) { throw new Exception("Customer not found."); } else { listCustomerNames.Items.Remove(listCustomerNames.SelectedItem); store.Delete(customer.ID); ClearAllFields(); MessageBox.Show("Customer deleted successfully."); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Handles the event when the Delete button is clicked. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDelete_Click(object sender, RoutedEventArgs e) { if (int.TryParse(txtCustomerId.Text, out int customerId)) { store.Delete(customerId); ApplyDatabinding(); ClearInputs(); } else { MessageBox.Show("Please enter a valid customer id"); } }
/// <summary> /// Deletes the customer by ID if such a customer exists & updates customer listbox to reflect change. /// </summary> /// <param name="customerID">The customer identifier.</param> private void DeleteCustomer(int customerID) { var foundCustomer = store.Find(customerID); if (foundCustomer == null) { MessageBox.Show("No customer with entered ID found"); } else { store.Delete(customerID); MessageBox.Show($"Customer with ID {customerID} deleted."); lstboxCustomerIDs.ItemsSource = store.IDs; lstboxCustomerIDs.UpdateLayout(); } }