/// <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> /// A method to remove the selected schedule record when the 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 selectedSessionId = Convert.ToInt32(this.dataGridViewSchedule.SelectedRows[0].Cells["SessionID"].Value.ToString()); // Confirm whether user truly wants to remove this scheduled block DialogResult confirmRemove = MessageBox.Show( @"Are you sure you want to remove this scheduled time?", @"Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); // If yes, remove the record if (confirmRemove.Equals(DialogResult.Yes)) { var session = new CoachSession { SessionID = selectedSessionId }; try { using (var context = new db_sft_2172Entities()) { context.CoachSessions.Attach(session); context.CoachSessions.Remove(session); context.SaveChanges(); MessageBox.Show(@"Delete successful!"); this.PopulateScheduleGrid(); } } 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> /// Method to query the database and display data for the desired session /// </summary> private void LoadSessionData() { try { using (var context = new db_sft_2172Entities()) { var sessionQuery = from session in context.CoachSessions where session.SessionID.Equals(this.CurrentSession.SessionID) select session; if (sessionQuery.Any()) { this.CurrentSession = sessionQuery.FirstOrDefault(); } else { MessageBox.Show(@"Sorry, could not load session data from the database. Please try again later."); this.Close(); } } } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); return; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } // Populate combo boxes with current session data this.cbxCoach.SelectedValue = this.CurrentSession.CoachID; this.cbxDay.SelectedValue = this.CurrentSession.DayID; this.cbxStartTime.SelectedValue = this.CurrentSession.StartTime; this.cbxEndTime.SelectedValue = this.CurrentSession.EndTime; this.cbxActive.SelectedIndex = this.CurrentSession.Active ? 0 : 1; }
/// <summary> /// Event handler to save session data and close form when the Save button is clicked /// </summary> /// <param name="sender">The parameter is not used.</param> /// <param name="e">The parameter is not used.</param> private void BtnSaveClick(object sender, EventArgs e) { if (this.CurrentSession == null) { this.CurrentSession = new CoachSession(); } // Get data from form and insert into current Session object this.CurrentSession.DayID = this.cbxDay.SelectedValue.ToString(); this.CurrentSession.CoachID = this.cbxCoach.SelectedValue.ToString(); // Get data from form and insert into current Availability object this.CurrentSession.DayID = this.cbxDay.SelectedValue.ToString(); // Get selected start time as a TimeSpan Time selectedStartTime = (Time)this.cbxStartTime.SelectedItem; this.CurrentSession.StartTime = selectedStartTime.Time1; // Get selected end time as a TimeSpan Time selectedEndTime = (Time)this.cbxEndTime.SelectedItem; this.CurrentSession.EndTime = selectedEndTime.Time1; // Update active status based on selected value this.CurrentSession.Active = this.cbxActive.SelectedIndex == 0; // Verify that the selected coach has availablility on the desired day/time try { using (var context = new db_sft_2172Entities()) { var availableCoachSessions = from coachAvailability in context.CoachAvailabilities where coachAvailability.CoachID.Equals(this.CurrentSession.CoachID) && coachAvailability.DayID.Equals(this.CurrentSession.DayID) && coachAvailability.StartTime <= this.CurrentSession.StartTime && coachAvailability.EndTime >= this.CurrentSession.EndTime select coachAvailability; if (availableCoachSessions.Any()) { // Call appropriate method to add or update session data if (this.SessionId == -1) { this.AddNewSession(); } else { this.UpdateSession(); } } else { MessageBox.Show( @"Sorry, the desired coach is not available for the selected day/time." + Environment.NewLine + @"Please select another combination!"); return; } } } catch (SqlException sqlEx) { MessageBox.Show(sqlEx.InnerException != null ? sqlEx.InnerException.Message : sqlEx.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } // Close form once finished this.Close(); }