// method to save doctor public int SaveDoctor(DBLibrary.Doctor doctor) { db.Entry(doctor).State = System.Data.Entity.EntityState.Added; int saveSuccess = db.SaveChanges(); return(saveSuccess); }
// method to run when doctor listing is selected private void lstDoctorList_SelectionChanged(object sender, SelectionChangedEventArgs e) { // run check to validate a listing is chosen if (lstDoctorList.SelectedIndex > 0) { // set dcotor from element chosen selectedDoctor = doctors.ElementAt(lstDoctorList.SelectedIndex); // enable submenu options submenuModifySelectedDoctor.IsEnabled = true; submenuDeleteSelectedDoctor.IsEnabled = true; // clear list when DB method is add if (dBOperation == DBOperation.ADD) { ClearDoctorDetails(); } // set text fields tbxFirstName.Text = selectedDoctor.FirstName; tbxLastName.Text = selectedDoctor.LastName; tbxAddress.Text = selectedDoctor.Address; tbxEmail.Text = selectedDoctor.Email; tbxPhone.Text = selectedDoctor.PhoneNo.ToString(); tbxOnDuty.Text = selectedDoctor.OnDuty.ToString(); tbxUserID.Text = selectedDoctor.UserID.ToString(); } }
// method to run when ok button clicked to save or modify doctor record private void btnOk_Click(object sender, RoutedEventArgs e) { if (dBOperation == DBOperation.ADD) { // set doctor data DBLibrary.Doctor doctor = new DBLibrary.Doctor(); doctor.FirstName = tbxFirstName.Text.Trim(); doctor.LastName = tbxLastName.Text.Trim(); doctor.Address = tbxAddress.Text.Trim(); doctor.Email = tbxEmail.Text.Trim(); doctor.PhoneNo = Int32.Parse(tbxPhone.Text.Trim()); doctor.OnDuty = Convert.ToBoolean(tbxOnDuty.Text.Trim()); doctor.UserID = Int32.Parse(tbxUserID.Text.Trim()); // save success as int int saveSuccess = SaveDoctor(doctor); if (saveSuccess == 1) { // print success message on save MessageBox.Show($"Doctor {doctor.FirstName} {doctor.LastName} has been added to the database!", "Save to Database", MessageBoxButton.OK, MessageBoxImage.Information); RefreshDoctorList(); ClearDoctorDetails(); stkDoctorDetails.Visibility = Visibility.Collapsed; } else { // print error message MessageBox.Show("Error saving doctor record.", "Save to Database", MessageBoxButton.OK, MessageBoxImage.Warning); } } // run if method enum is set to MODIFY if (dBOperation == DBOperation.MODIFY) { foreach (var doctor in db.Doctors.Where(t => t.UserID == selectedDoctor.UserID)) { doctor.FirstName = tbxFirstName.Text.Trim(); doctor.LastName = tbxLastName.Text.Trim(); doctor.Address = tbxAddress.Text.Trim(); doctor.Email = tbxEmail.Text.Trim(); doctor.PhoneNo = Int32.Parse(tbxPhone.Text.Trim()); doctor.OnDuty = Convert.ToBoolean(tbxOnDuty.Text.Trim()); doctor.UserID = Int32.Parse(tbxUserID.Text.Trim()); } // save success as int int save = db.SaveChanges(); if (save == 1) { // print success MessageBox.Show("Doctor modified successfully!", "Save to Database", MessageBoxButton.OK, MessageBoxImage.Information); RefreshDoctorList(); // refresh dcotor list ClearDoctorDetails(); // clear doctor details stkDoctorDetails.Visibility = Visibility.Collapsed; // collapse stackpanel } } }