/// <summary> /// Handle when the save button is clicked. Saves the invoice if valid /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveInvoice_Click(object sender, RoutedEventArgs e) { try { if (string.IsNullOrWhiteSpace(invoice.sInvoiceDate)) { MessageBox.Show("An invoice must have a date before it can be saved."); return; } else if (string.IsNullOrWhiteSpace(invoice.sTotalCost) || Convert.ToInt32(invoice.sTotalCost) == 0) { MessageBox.Show("An invoice must have items before it can be saved."); return; } bool invoiceSaved; if (isNewInvoice) { invoiceSaved = sqlProvider.InsertInvoice(invoice); //get the max invoice that was recently saved invoice.sInvoiceNum = sqlProvider.GetMaxInvoiceID(); if (string.IsNullOrWhiteSpace(invoice.sInvoiceNum)) { throw new Exception("Failed to retrieve the invoice number after saving"); } lblInvoiceNum.Content = $"Invoice #{invoice.sInvoiceNum}"; } else { invoiceSaved = sqlProvider.UpdateInvoice(invoice); } if (!invoiceSaved) { MessageBox.Show("An error occurred while saving the invoice, changes have not been saved."); return; } //assume this is the case for instances like new invoices bool isOkToSaveItems = true; if (!isNewInvoice) { isOkToSaveItems = sqlProvider.DeleteInvoiceLineItems(invoice); } if (!isOkToSaveItems) { MessageBox.Show("An error occurred while attempting to reset the line items in the database. Invoice saved, items not."); return; } bool itemsSaved = sqlProvider.SaveAllLineItems(invoice.sInvoiceNum, lineItems); if (itemsSaved) { MessageBox.Show("Invoice saved successfully!"); isNewInvoice = false; btnEditInvoice.IsEnabled = true; btnDeleteInvoice.IsEnabled = true; LockInvoice(); } else { MessageBox.Show("An error occurred while attempting to save the line items in the database. Invoice saved, items not. Previous items still saved."); } } catch (Exception ex) { //This is the top level method so we want to handle the exception HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex.Message); } }