/// <summary> /// Method to create a new session in the database /// </summary> private void AddNewSession() { // Add new user to the database and save changes try { using (var context = new db_sft_2172Entities()) { // Run query and save new user data to database context.CoachSessions.Add(this.CurrentSession); context.SaveChanges(); } } catch (DbUpdateException dbUEx) { MessageBox.Show(dbUEx.InnerException != null ? dbUEx.InnerException.Message : dbUEx.Message); return; } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); return; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } MessageBox.Show(@"Save completed...new session created successfully!"); }
/// <summary> /// Event handler to click to save the new password into the database /// </summary> /// <param name="sender">The parameter is not used.</param> /// <param name="e">The parameter is not used.</param> private void BtnSaveNewPasswordClick(object sender, EventArgs e) { try { using (var context = new db_sft_2172Entities()) { var userQuery = from u in context.Users where u.UserID.Equals(Program.CurrentUser) select u; var userResult = userQuery.FirstOrDefault(); if (SaltedHash.Verify(userResult.PasswordSalt, userResult.Password, this.txtCurrentPassword.Text)) { if (!string.IsNullOrEmpty(this.txtNewPassword.Text) || !string.IsNullOrEmpty(this.txtConfirmPassword.Text)) { if (this.txtNewPassword.Text == this.txtConfirmPassword.Text) { // Generate salt and salted hash SaltedHash sh = new SaltedHash(this.txtNewPassword.Text); userResult.Password = sh.Hash; userResult.PasswordSalt = sh.Salt; userResult.ResetPassword = null; context.SaveChanges(); this.txtCurrentPassword.Text = string.Empty; this.txtNewPassword.Text = string.Empty; this.txtConfirmPassword.Text = string.Empty; MessageBox.Show(@"Your passsword has been saved!"); this.Close(); } else { MessageBox.Show(@"Passwords do not match!"); } } else { MessageBox.Show(@"New password or confirm password is empty!"); } } else { MessageBox.Show(@"Your current password is incorrect!"); } } } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Method to update an existing session in the database /// </summary> private void UpdateAvailability() { // Add new user to the database and save changes try { using (var context = new db_sft_2172Entities()) { // Run query and pull matching session from database var availabilityQuery = from availability in context.CoachAvailabilities where availability.CoachAvailabilityID.Equals(this.CoachAvailabilityId) select availability; if (availabilityQuery.Any()) { CoachAvailability foundAvailability = availabilityQuery.FirstOrDefault(); // Update database records if (foundAvailability != null) { foundAvailability.CoachID = this.CurrentAvailability.CoachID; foundAvailability.DayID = this.CurrentAvailability.DayID; foundAvailability.StartTime = this.CurrentAvailability.StartTime; foundAvailability.EndTime = this.CurrentAvailability.EndTime; } // Save changes to database context.SaveChanges(); } else { MessageBox.Show( @"Sorry, a matching session was not found in the database.\nPlease try again or contact an administrator for assistance."); } } } catch (DbUpdateException dbUEx) { MessageBox.Show(dbUEx.InnerException != null ? dbUEx.InnerException.Message : dbUEx.Message); return; } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); return; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } MessageBox.Show(@"Save completed...Session updated successfully!"); }
/// <summary> /// Method to update an existing session in the database /// </summary> private void UpdateSession() { // Add new session to the database and save changes try { using (var context = new db_sft_2172Entities()) { // Run query and pull matching session from database var sessionQuery = from session in context.CoachSessions where session.SessionID.Equals(this.CurrentSession.SessionID) select session; if (sessionQuery.Any()) { CoachSession foundSession = sessionQuery.FirstOrDefault(); // Update database records foundSession.DayID = this.CurrentSession.DayID; foundSession.StartTime = this.CurrentSession.StartTime; foundSession.EndTime = this.CurrentSession.EndTime; foundSession.CoachID = this.CurrentSession.CoachID; foundSession.Active = this.CurrentSession.Active; // Save changes to database context.SaveChanges(); } else { MessageBox.Show( @"Sorry, the desired session was not found in the database." + Environment.NewLine + @"Please try again later or contact an administrator for assistance."); } } } catch (DbUpdateException dbUEx) { MessageBox.Show(dbUEx.InnerException != null ? dbUEx.InnerException.Message : dbUEx.Message); return; } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); return; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } MessageBox.Show(@"Save completed...Session updated successfully!"); }
/// <summary> /// Event handler to delete the selected Availability record when the Remove button is clicked /// </summary> /// <param name="sender">The parameter is not used.</param> /// <param name="e">The parameter is not used.</param> private void BtnRemove_Click(object sender, EventArgs e) { // Determine which row is selected int selectedAvailabilityId = Convert.ToInt32(this.dataGridViewAvailability.SelectedRows[0].Cells["CoachAvailabilityID"].Value.ToString()); // Confirm whether user truly wants to remove this availability record DialogResult confirmRemove = MessageBox.Show( @"Are you sure you want to remove this record?", @"Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); // If yes, remove the record if (confirmRemove.Equals(DialogResult.Yes)) { var availability = new CoachAvailability { CoachAvailabilityID = selectedAvailabilityId }; try { using (var context = new db_sft_2172Entities()) { context.CoachAvailabilities.Attach(availability); context.CoachAvailabilities.Remove(availability); context.SaveChanges(); MessageBox.Show(@"Delete successful!"); this.PopulateAvailabilityGrid(); } } catch (DbUpdateException dbUEx) { MessageBox.Show(dbUEx.InnerException != null ? dbUEx.InnerException.Message : dbUEx.Message); } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
/// <summary> /// Submit button sends the added or updated info to the database. /// </summary> /// <param name="sender">The parameter is not used.</param> /// <param name="e">The parameter is not used.</param> private void BtnSubmitClick(object sender, EventArgs e) { try { // Run query to check for a corresponding user in the database using (var context = new db_sft_2172Entities()) { string courseId = this.txtCourseID.Text; var courseQuery = from course in context.Courses where course.CourseID.Equals(courseId) select course; if (courseQuery.Any()) { var courseResult = courseQuery.FirstOrDefault(); if (courseResult != null) { courseResult.CourseID = this.txtCourseID.Text; courseResult.CourseName = this.txtCourseName.Text; Department selectedDepartment = (Department)this.cbxDepartment.SelectedItem; courseResult.DepartmentID = selectedDepartment.DepartmentID; } context.SaveChanges(); this.DisplayCourses(); MessageBox.Show(@"Course Updated"); } else { Course newCourse = new Course { CourseID = this.txtCourseID.Text, CourseName = this.txtCourseName.Text, Department = (Department)this.cbxDepartment.SelectedItem, IsActive = true }; context.Courses.Add(newCourse); context.SaveChanges(); MessageBox.Show(@"Course Added"); // If save is successful, update the user list and display the new user profile this.DisplayCourses(); this.cbxChooseCourse.SelectedValue = newCourse.CourseID; } } } catch (DbUpdateException dbUEx) { MessageBox.Show(dbUEx.InnerException != null ? dbUEx.InnerException.Message : dbUEx.Message); } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } this.txtCourseID.Enabled = false; }
/// <summary> /// Event handler to set temporary access code /// </summary> /// <param name="sender">The parameter is not used.</param> /// <param name="e">The parameter is not used.</param> private void BtnUpdateClick(object sender, EventArgs e) { // Verify that the two entered passwords match if (!this.txtTempCode.Text.Equals(this.txtConfirmTempCode.Text)) { MessageBox.Show(@"Sorry, the temporary passwords do not match. Please try again!"); // Clear the password boxes this.txtTempCode.Text = string.Empty; this.txtConfirmTempCode.Text = string.Empty; this.txtTempCode.Focus(); } else if (this.txtTempCode.Text.Equals(string.Empty)) { MessageBox.Show(@"Please enter a temporary password."); // Clear the password boxes this.txtTempCode.Text = string.Empty; this.txtConfirmTempCode.Text = string.Empty; this.txtTempCode.Focus(); } else { // Find current user, then update password in database try { using (var context = new db_sft_2172Entities()) { // Run query to get user data var userQuery = from users in context.Users where users.UserID.Equals(this.CurrentUserId) select users; User currentUser = userQuery.FirstOrDefault(); if (currentUser != null) { // Generate salt and salted hash SaltedHash sh = new SaltedHash(this.txtTempCode.Text); currentUser.Password = sh.Hash; currentUser.PasswordSalt = sh.Salt; currentUser.ResetPassword = "******"; context.SaveChanges(); // Show confirmation if save is successful MessageBox.Show(@"Temporary password updated successfully!"); } } } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } // Close the form when finished this.Close(); } }
/// <summary> /// Submit button sends the added or updated info to the database. /// </summary> /// <param name="sender">The parameter is not used.</param> /// <param name="e">The parameter is not used.</param> private void BtnSubmitClick(object sender, EventArgs e) { List <ViewCoachCours> addList = new List <ViewCoachCours>(); List <ViewCoachCours> removeList = new List <ViewCoachCours>(); // Loop through selected courses and determine whether selected courses // need to be added to the list foreach (ViewCoachCours selectedCourse in this.currentSelectedCourseList) { bool isCourseSelected = false; foreach (ViewCoachCours coachCourse in this.coachSelectedCourseList) { if (coachCourse.CourseID.Equals(selectedCourse.CourseID)) { isCourseSelected = true; break; } } if (!isCourseSelected) { addList.Add(selectedCourse); } } // Loop through the originally selected courses and determine whether // any were removed foreach (ViewCoachCours coachCourse in this.coachSelectedCourseList) { bool isCourseSelected = false; foreach (ViewCoachCours selectedCourse in this.currentSelectedCourseList) { if (coachCourse.CourseID.Equals(selectedCourse.CourseID)) { isCourseSelected = true; break; } } if (!isCourseSelected) { removeList.Add(coachCourse); } } try { // Query updates the user in the database using (var context = new db_sft_2172Entities()) { string coachId = this.txtID.Text; var coachQuery = from coach in context.Coaches where coach.CoachID.Equals(coachId) select coach; if (coachQuery.Any()) { var coachResult = coachQuery.FirstOrDefault(); coachResult.FirstName = this.txtFirstName.Text; coachResult.MiddleName = this.txtMiddleName.Text; coachResult.LastName = this.txtLastName.Text; coachResult.DisplayName = this.txtDisplayName.Text; coachResult.Phone = this.txtPhone.Text; coachResult.Email = this.txtEmail.Text; coachResult.IsActive = this.chkActive.Checked; coachResult.SupervisorID = this.cbxSupervisor.SelectedIndex == -1 ? string.Empty : this.cbxSupervisor.SelectedValue.ToString(); foreach (ViewCoachCours course in addList) { CoachCourse newCourse = new CoachCourse { CoachID = this.txtID.Text, CourseID = course.CourseID, Active = true }; context.CoachCourses.Add(newCourse); } foreach (ViewCoachCours course in removeList) { CoachCourse dropCourse = new CoachCourse { CoachID = this.txtID.Text, CourseID = course.CourseID }; context.CoachCourses.Attach(dropCourse); context.CoachCourses.Remove(dropCourse); } context.SaveChanges(); MessageBox.Show(@"Coach Profile Updated"); this.GetCoachSelectedCourses(); this.DisplaySelectedCourses(); this.DisplayUnselectedCourses(); } else { Coach newCoach = new Coach { CoachID = this.txtID.Text, FirstName = this.txtFirstName.Text, MiddleName = this.txtMiddleName.Text, LastName = this.txtLastName.Text, DisplayName = this.txtDisplayName.Text, Phone = this.txtPhone.Text, Email = this.txtEmail.Text, IsActive = this.chkActive.Checked, SupervisorID = this.cbxSupervisor.SelectedIndex == -1 ? string.Empty : this.cbxSupervisor.SelectedValue.ToString() }; foreach (ViewCoachCours course in addList) { CoachCourse newCourse = new CoachCourse { CoachID = this.txtID.Text, CourseID = course.CourseID, Active = true }; context.CoachCourses.Add(newCourse); } foreach (ViewCoachCours course in removeList) { CoachCourse dropCourse = new CoachCourse { CoachID = this.txtID.Text, CourseID = course.CourseID }; context.CoachCourses.Attach(dropCourse); context.CoachCourses.Remove(dropCourse); } context.Coaches.Add(newCoach); context.SaveChanges(); MessageBox.Show(@"Coach Profile Added"); // If save is successful, update the coach list and display the new coach profile this.DisplayCoaches(); this.cbxChooseCoach.SelectedValue = newCoach.CoachID; this.txtID.Enabled = false; this.GetCoachSelectedCourses(); this.DisplaySelectedCourses(); this.DisplayUnselectedCourses(); } } } catch (DbUpdateException dbUEx) { MessageBox.Show(dbUEx.InnerException != null ? dbUEx.InnerException.Message : dbUEx.Message); } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Submit button sends the added or updated info to the database. /// </summary> /// <param name="sender">The parameter is not used.</param> /// <param name="e">The parameter is not used.</param> private void BtnSubmitClick(object sender, EventArgs e) { try { // Run query to check for a corresponding user in the database using (var context = new db_sft_2172Entities()) { string userId = this.txtID.Text; var userQuery = from user in context.Users where user.UserID.Equals(userId) select user; if (userQuery.Any()) { var userResult = userQuery.FirstOrDefault(); userResult.FirstName = this.txtFirstName.Text; userResult.MiddleName = this.txtMiddleName.Text; userResult.LastName = this.txtLastName.Text; userResult.DisplayName = this.txtDisplayName.Text; userResult.Phone = this.txtPhone.Text; userResult.Email = this.txtEmail.Text; userResult.IsActive = this.chkActive.Checked; userResult.IsAdmin = this.chkAdmin.Checked; userResult.IsSupervisor = this.chkSupervisor.Checked; context.SaveChanges(); MessageBox.Show(@"User Profile Updated"); } else { User newUser = new User { UserID = this.txtID.Text, FirstName = this.txtFirstName.Text, MiddleName = this.txtMiddleName.Text, LastName = this.txtLastName.Text, DisplayName = this.txtDisplayName.Text, Phone = this.txtPhone.Text, Email = this.txtEmail.Text, IsActive = this.chkActive.Checked, IsAdmin = this.chkAdmin.Checked, IsSupervisor = this.chkSupervisor.Checked }; context.Users.Add(newUser); context.SaveChanges(); MessageBox.Show(@"User Profile Created"); // If save is successful, update the user list and display the new user profile this.DisplayUsers(); this.cbxChooseUser.SelectedValue = newUser.UserID; this.txtID.Enabled = false; this.btnAdd.Enabled = true; } } } catch (DbUpdateException dbUEx) { MessageBox.Show(dbUEx.InnerException != null ? dbUEx.InnerException.Message : dbUEx.Message); return; } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); return; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }