protected void RemoveCourseButton_Click(object sender, EventArgs e) { using (var context = new SchoolEntities()) { var instructorID = Convert.ToInt32(InstructorsDropDownList.SelectedValue); var instructor = (from p in context.People where p.PersonID == instructorID select p).First(); var courseID = Convert.ToInt32(AssignedCoursesDropDownList.SelectedValue); var courses = instructor.Courses; var courseToRemove = new Course(); foreach (Course c in courses) { if (c.CourseID == courseID) { courseToRemove = c; break; } } try { courses.Remove(courseToRemove); context.SaveChanges(); PopulateDropDownLists(); CourseRemovedLabel.Text = "Removal successful."; } catch (Exception) { CourseRemovedLabel.Text = "Removal unsuccessful."; //Add code to log the error. } CourseRemovedLabel.Visible = true; } }
protected void AssignCourseButton_Click(object sender, EventArgs e) { using (var context = new SchoolEntities()) { var instructorID = Convert.ToInt32(InstructorsDropDownList.SelectedValue); var instructor = (from p in context.People where p.PersonID == instructorID select p).First(); var courseID = Convert.ToInt32(UnassignedCoursesDropDownList.SelectedValue); var course = (from c in context.Courses where c.CourseID == courseID select c).First(); instructor.Courses.Add(course); try { context.SaveChanges(); PopulateDropDownLists(); CourseAssignedLabel.Text = "Assignment successful."; } catch (Exception) { CourseAssignedLabel.Text = "Assignment unsuccessful."; //Add code to log the error. } CourseAssignedLabel.Visible = true; } }
protected void InstructorsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) { using (var context = new SchoolEntities()) { var instructorBeingUpdated = Convert.ToInt32(e.Keys[0]); var officeAssignment = (from o in context.OfficeAssignments where o.InstructorID == instructorBeingUpdated select o).FirstOrDefault(); try { if (String.IsNullOrWhiteSpace(instructorOfficeTextBox.Text) == false) { if (officeAssignment == null) { context.OfficeAssignments.AddObject( OfficeAssignment.CreateOfficeAssignment( instructorBeingUpdated, instructorOfficeTextBox.Text, null)); } else { officeAssignment.Location = instructorOfficeTextBox.Text; } } else { if (officeAssignment != null) { context.DeleteObject(officeAssignment); } } context.SaveChanges(); } catch (Exception) { e.Cancel = true; ErrorMessageLabel.Visible = true; ErrorMessageLabel.Text = "Update failed."; //Add code to log the error. } } }