public int SaveNurse(DBLibrary.Nurse nurse) { db.Entry(nurse).State = System.Data.Entity.EntityState.Added; int saveSuccess = db.SaveChanges(); return(saveSuccess); }
private void btnOk_Click(object sender, RoutedEventArgs e) { if (dBOperation == DBOperation.ADD) { DBLibrary.Nurse nurse = new DBLibrary.Nurse(); nurse.FirstName = tbxFirstName.Text.Trim(); nurse.LastName = tbxLastName.Text.Trim(); nurse.Address = tbxAddress.Text.Trim(); nurse.Email = tbxEmail.Text.Trim(); nurse.PhoneNo = Int32.Parse(tbxPhone.Text.Trim()); nurse.OnDuty = Convert.ToBoolean(tbxOnDuty.Text.Trim()); nurse.UserID = Int32.Parse(tbxUserID.Text.Trim()); int saveSuccess = SaveNurse(nurse); if (saveSuccess == 1) { MessageBox.Show($"Nurse {nurse.FirstName} {nurse.LastName} has been added to the database!", "Save to Database", MessageBoxButton.OK, MessageBoxImage.Information); RefreshNurseList(); ClearNurseDetails(); stkNurseDetails.Visibility = Visibility.Collapsed; } else { MessageBox.Show("Error saving nurse record.", "Save to Database", MessageBoxButton.OK, MessageBoxImage.Warning); } } if (dBOperation == DBOperation.MODIFY) { foreach (var nurse in db.Nurses.Where(t => t.UserID == selectedNurse.UserID)) { nurse.FirstName = tbxFirstName.Text.Trim(); nurse.LastName = tbxLastName.Text.Trim(); nurse.Address = tbxAddress.Text.Trim(); nurse.Email = tbxEmail.Text.Trim(); nurse.PhoneNo = Int32.Parse(tbxPhone.Text.Trim()); nurse.OnDuty = Convert.ToBoolean(tbxOnDuty.Text.Trim()); nurse.UserID = Int32.Parse(tbxUserID.Text.Trim()); } int save = db.SaveChanges(); if (save == 1) { MessageBox.Show("Nurse modified successfully!", "Save to Database", MessageBoxButton.OK, MessageBoxImage.Information); RefreshNurseList(); ClearNurseDetails(); stkNurseDetails.Visibility = Visibility.Collapsed; } } }
private void lstNurseList_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (lstNurseList.SelectedIndex > 0) { selectedNurse = nurses.ElementAt(lstNurseList.SelectedIndex); submenuModifySelectedNurse.IsEnabled = true; submenuDeleteSelectedNurse.IsEnabled = true; if (dBOperation == DBOperation.ADD) { ClearNurseDetails(); } tbxFirstName.Text = selectedNurse.FirstName; tbxLastName.Text = selectedNurse.LastName; tbxAddress.Text = selectedNurse.Address; tbxEmail.Text = selectedNurse.Email; tbxPhone.Text = selectedNurse.PhoneNo.ToString(); tbxOnDuty.Text = selectedNurse.OnDuty.ToString(); tbxUserID.Text = selectedNurse.UserID.ToString(); } }