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 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 ExecuteButton_Click(object sender, EventArgs e)
 {
     using (SchoolEntities context = new SchoolEntities())
     {
         RowsAffectedLabel.Text = context.ExecuteStoreCommand("UPDATE Course SET Credits = Credits * {0}", CreditsMultiplierTextBox.Text).ToString();
     }
 }
 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.
         }
     }
 }
        private void PopulateDropDownLists()
        {
            using (var context = new SchoolEntities())
            {
                var allCourses = context.GetCourses().ToList();

                var instructorID = Convert.ToInt32(InstructorsDropDownList.SelectedValue);
                var instructor = (from p in context.People.Include("Courses")
                                  where p.PersonID == instructorID
                                  select p).First();

                var assignedCourses = instructor.Courses.AsEnumerable();
                var unassignedCourses = allCourses.Except(assignedCourses.AsEnumerable()).ToList();

                UnassignedCoursesDropDownList.DataSource = unassignedCourses;
                UnassignedCoursesDropDownList.DataBind();
                UnassignedCoursesDropDownList.Visible = true;

                AssignedCoursesDropDownList.DataSource = assignedCourses;
                AssignedCoursesDropDownList.DataBind();
                AssignedCoursesDropDownList.Visible = true;
            }
        }