private void btnDeleteInvoice_Click(object sender, EventArgs e) { if (invoiceIDTextBox.Text == "") { MessageBox.Show("No invoice selected. Please select an invoice to delete.", "Delete Error"); } else { int currentInv = Convert.ToInt32(invoiceIDTextBox.Text); var editedInv = (from invoice in invoicesDb.Invoices where invoice.InvoiceID == currentInv select invoice).Single(); DialogResult result = MessageBox.Show($"Delete Invoice ID: {currentInv.ToString()}?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { invoicesDb.Invoices.Remove(editedInv); invoicesDb.SaveChanges(); } catch (DbUpdateConcurrencyException) { this.Close(); if (invoicesDb.Entry(editedInv).State == EntityState.Detached) { MessageBox.Show("Another user has deleted that invoice.", "Concurrency Error"); } else { MessageBox.Show("Another user has updated that invoice.", "Concurrency Error"); } } catch (DbUpdateException) { this.Close(); MessageBox.Show("Unable to delete invoice. Unknown Error Occurred.", "Invoice Not Deleted"); InvoiceListForm newForm = new InvoiceListForm(); newForm.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
private void btnVendorsFormDeleteVend_Click(object sender, EventArgs e) { if (vendorIDTextBox.Text == "") { MessageBox.Show("No vendor selected. Please select a vendor to delete.", "Delete Error"); } else { int currentVend = Convert.ToInt32(vendorIDTextBox.Text); var editedVend = (from vendor in VendorDb.Vendors where vendor.VendorID == currentVend select vendor).Single(); DialogResult result = MessageBox.Show($"Delete {editedVend.VendorName}?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { VendorDb.Vendors.Remove(editedVend); VendorDb.SaveChanges(); } catch (DbUpdateConcurrencyException) { this.Close(); if (VendorDb.Entry(editedVend).State == EntityState.Detached) { MessageBox.Show("Another user has deleted that vendor.", "Concurrency Error"); } else { MessageBox.Show("Another user has updated that vendor.", "Concurrency Error"); } VendorsListForm newForm = new VendorsListForm(); newForm.Show(); } catch (DbUpdateException) { this.Close(); MessageBox.Show("Unable to delete vendor. The vendor has records related in the purchase order table.", "Vendor Not Deleted"); VendorsListForm newForm = new VendorsListForm(); newForm.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
//Deletes customer private void btnCustomerFormDeleteCust_Click(object sender, EventArgs e) { if (customerIDTextBox.Text == "") { MessageBox.Show("No customer selected. Please select a customer to delete.", "Delete Error"); } else { int currentCust = Convert.ToInt32(customerIDTextBox.Text); var editedCust = (from customer in CustDb.Customers where customer.CustomerID == currentCust select customer).Single(); DialogResult result = MessageBox.Show($"Delete {editedCust.CustomerFName} {editedCust.CustomerLName}?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { CustDb.Customers.Remove(editedCust); CustDb.SaveChanges(); } catch (DbUpdateConcurrencyException) { this.Close(); if (CustDb.Entry(editedCust).State == EntityState.Detached) { MessageBox.Show("Another user has deleted that customer.", "Concurrency Error"); } else { MessageBox.Show("Another user has updated that customer.", "Concurrency Error"); } CustomersListForm newForm = new CustomersListForm(); newForm.Show(); } catch (DbUpdateException) { this.Close(); MessageBox.Show("Unable to delete customer. The customer has invoices in the invoices table.", "Customer Not Deleted"); CustomersListForm newForm = new CustomersListForm(); newForm.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
//Deletes Employee private void btnEmployeeFormDeleteEmp_Click(object sender, EventArgs e) { if (employeeIDTextBox.Text == "") { MessageBox.Show("No employee selected. Please select an employee to delete", "Delete Error"); } else { int currentEmp = Convert.ToInt32(employeeIDTextBox.Text); var editedEmp = (from employee in EmpDb.Employees where employee.EmployeeID == currentEmp select employee).Single(); DialogResult result = MessageBox.Show($"Delete {editedEmp.EmployeeFName} {editedEmp.EmployeeLName}?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { EmpDb.Employees.Remove(editedEmp); EmpDb.SaveChanges(); } catch (DbUpdateConcurrencyException) { this.Close(); if (EmpDb.Entry(editedEmp).State == EntityState.Detached) { MessageBox.Show("Another user has deleted that employee.", "Concurrency Error"); } else { MessageBox.Show("Another user has updated that employee.", "Concurrency Error"); } } catch (DbUpdateException) { this.Close(); MessageBox.Show("Unable to delete employee. The employee is labeled as an employee that worked on an invoice.", "Employee Not Deleted"); EmployeesListForm newForm = new EmployeesListForm(); newForm.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
private void btnServicesFormDeleteService_Click(object sender, EventArgs e) { if (serviceIDTextBox.Text == "") { MessageBox.Show("No service selected. Please select a service to delete.", "Delete Error"); } else { int currentService = Convert.ToInt32(serviceIDTextBox.Text); var editedService = (from service in ServiceDb.Services where service.ServiceID == currentService select service).Single(); DialogResult result = MessageBox.Show($"Delete Service ID: {editedService.ServiceID} | Name: {editedService.ServiceName}?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { ServiceDb.Services.Remove(editedService); ServiceDb.SaveChanges(); } catch (DbUpdateConcurrencyException) { this.Close(); if (ServiceDb.Entry(editedService).State == EntityState.Detached) { MessageBox.Show("Another user has deleted that employee.", "Concurrency Error"); } else { MessageBox.Show("Another user has updated that employee.", "Concurrency Error"); } } catch (DbUpdateException) { this.Close(); MessageBox.Show("Unable to delete service. The service is shown to be used on invoices.", "Service Not Deleted"); ServicesListForm newForm = new ServicesListForm(); newForm.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }