private void NewBtn_Click(object sender, RoutedEventArgs e) { try { if (mainVM.Patient.IsValidRecord()) { string msg = "The application contains patient data. Would you like to save the existing " + "data before clearing it?" + Environment.NewLine + Environment.NewLine + "Click YES to save the existing data and NO to clear it without saving."; MessageBoxResult result = MessageBox.Show(msg, "Confirm Saving Before Clearing", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { SaveDatabaseType saveType = SaveRecordToDatabase(); if (saveType == SaveDatabaseType.ADDED_NEW || saveType == SaveDatabaseType.MODIFIED_EXISTING) { mainVM.InitializeProperties(); } else { //could not save the existing valid patient data, so dont' clear it. } } else { //clear existing data without saving mainVM.InitializeProperties(); } } else { //invalid account number that can't be saved, so clear it mainVM.InitializeProperties(); } } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } }
private SaveDatabaseType SaveRecordToDatabase() { SaveDatabaseType saveType = SaveDatabaseType.NO_ACTION; if (mainVM.Patient.IsValidRecord()) { MainViewModel.OpenDbConnection(true); try { string accountNumber = mainVM.Patient.AccNumber; string examDate = mainVM.Patient.DateOfExam.ToString(CultureInfo.CurrentCulture); string sqliteExamDate = SqliteDateTimeHelper.ToSqliteDateTime(mainVM.Patient.DateOfExam); bool recordExists = mainVM.DoesRecordExistInDatabase(accountNumber, sqliteExamDate); if (!recordExists) { string error; string msg = string.Empty; bool success = mainVM.AddRecordToDatabase(out error); MainViewModel.CloseDbConnection(); if (success) { saveType = SaveDatabaseType.ADDED_NEW; msg = "Patient record with account number " + accountNumber + " and date of exam " + examDate + " written to the database."; } else { msg = "Error: Failed to write patient record with account number " + accountNumber + " and date of exam " + examDate + " to the database. " + error; } MessageBox.Show(msg); } else { string msg = "A patient record with account number " + accountNumber + " and date of exam " + examDate + " already exists in the database. Would you like to overwrite the existing " + "record with the new data or create a new record?" + Environment.NewLine + Environment.NewLine + "Click YES to overwrite the existing data and NO to create a new record." + " In order to create a new record you will have to change the exam date or account number."; MessageBoxResult result = MessageBox.Show(msg, "Confirm Overwrite", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { string error; msg = string.Empty; bool success = mainVM.UpdateRecordInDatabase(out error); MainViewModel.CloseDbConnection(); if (success) { saveType = SaveDatabaseType.MODIFIED_EXISTING; msg = "Patient record with account number " + accountNumber + " and date of exam " + examDate + " updated in the database."; } else { msg = "Error: Failed to update patient record with account number " + accountNumber + " and date of exam " + examDate + " to the database. " + error; } MessageBox.Show(msg); } else if (result == MessageBoxResult.No) { saveType = SaveDatabaseType.NO_ACTION; } } } catch (Exception ex) { MainViewModel.CloseDbConnection(); MessageBox.Show("Exception thrown while writing patient record to the database." + Environment.NewLine + Environment.NewLine + "Exception Message: " + ex.Message + Environment.NewLine + Environment.NewLine + "Exception Stack Trace: " + ex.StackTrace); } } else { MessageBox.Show("Invalid patient record with account number " + mainVM.Patient.AccNumber + " . Cannot save to the database."); } return(saveType); }