/**
         * <summary>
         * This method gets the student data from the DB
         * </summary>
         *
         * @method GetStudents
         * @returns {void}
         */
        protected void GetStudents()
        {
            // Connect to EF
            using (ContosoConnection db = new ContosoConnection())
            {
                string SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();

                // Query the Students Table using EF and LINQ
                var Students = (from allStudents in db.Students
                                select allStudents);

                // Bind the result to the GridView
                StudentsGridView.DataSource = Students.AsQueryable().OrderBy(SortString).ToList();
                StudentsGridView.DataBind();
            }
        }
        protected void GetStudent()
        {
            // Populate the form with existing data from the database
            int StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

            // Connect to the EF DB
            using (ContosoConnection db = new ContosoConnection())
            {
                // Populate a student object instance with the StudentId from the URL parameter
                Student updatedStudent = (from student in db.Students
                                          where student.StudentID == StudentID
                                          select student).FirstOrDefault();

                // Map the student properties to the form controls
                if (updatedStudent != null)
                {
                    LastNameTextBox.Text = updatedStudent.LastName;
                    FirstNameTextBox.Text = updatedStudent.FirstMidName;
                    EnrollmentDateTextBox.Text = updatedStudent.EnrollmentDate.ToString("yyyy-MM-dd");
                }
            }
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            // Use EF to connect to the server
            using (ContosoConnection db = new ContosoConnection())
            {
                // Use the Student model to create a new student object and save a new record
                Student newStudent = new Student();

                int StudentID = 0;

                if (Request.QueryString.Count > 0) // Our URL has a StudentID in it
                {
                    // Get the ID from the URL
                    StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                    // Get the current student from EF DB
                    newStudent = (from student in db.Students
                                  where student.StudentID == StudentID
                                  select student).FirstOrDefault();
                }
                // Add data to the new student record
                newStudent.LastName = LastNameTextBox.Text;
                newStudent.FirstMidName = FirstNameTextBox.Text;
                newStudent.EnrollmentDate = Convert.ToDateTime(EnrollmentDateTextBox.Text);

                // Use LINQ to ADO.NET to add or insert new student into the database
                if (StudentID == 0)
                {
                    db.Students.Add(newStudent);
                }

                // Save our changes
                db.SaveChanges();

                // Redirect back to the updated students page
                Response.Redirect("~/Students.aspx");
            }
        }
        /**
         * <summary>
         * This event handler deletes a student from the db using EF
         * </summary>
         *
         * @method StudentsGridView_RowDeleting
         * @param {object} sender
         * @param {GridViewDeleteEventArgs} e
         * @returns {void}
         */
        protected void StudentsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Store which row was clicked
            int selectedRow = e.RowIndex;

            // Get the selected StudentID using the Grid's DataKey collection
            int StudentID = Convert.ToInt32(StudentsGridView.DataKeys[selectedRow].Values["StudentID"]);

            // Use EF to find the selected student in the DB and remove it
            using (ContosoConnection db = new ContosoConnection())
            {
                // Create object of the Student class and store the query string inside of it
                Student deletedStudent = (from studentRecords in db.Students
                                          where studentRecords.StudentID == StudentID
                                          select studentRecords).FirstOrDefault();

                // Remove the selected student from the db
                db.Students.Remove(deletedStudent);

                // Save my changes back to the database
                db.SaveChanges();

                //refresh the Grid
                this.GetStudents();
            }
        }
        /**
         * <summary>
         * This event handler deletes a department from the databse using EF
         * </summary>
         * @method DepartmentsGridView_RowDeleting
         * @param {object} sender
         * @param {GridViewDeleteEventArgs}
         * @returns {void}
         * */
        protected void DepartmentsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Store which row was clicked
            int selectedRow = e.RowIndex;

            // Get the selected DepartmentID using the grids datakey collection
            int DepartmentID = Convert.ToInt32(DepartmentsGridView.DataKeys[selectedRow].Values["DepartmentID"]);

            // Use ef to find the selelcted Department and delete it
            using (ContosoConnection db = new ContosoConnection())
            {
                // Create object of the department class and store the query string inside of it
                Department deletedDepartment = (from departmentRecords in db.Departments
                                                where departmentRecords.DepartmentID == DepartmentID
                                                select departmentRecords).FirstOrDefault();

                // Remove the selected department from the db
                db.Departments.Remove(deletedDepartment);

                // Save db changes
                db.SaveChanges();

                // Refresh gridview
                this.GetDepartments();

            }
        }
        /**
         * <summary>
         * This method gets the department data from the database
         * </summary>
         * @method GetDepartments
         * @return {void}
         * */
        protected void GetDepartments()
        {
            // Connect to EF
            using (ContosoConnection db = new ContosoConnection())
            {
                string SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();
                // Query the departments table using EF and LINQ
                var Departments = (from allDepartments in db.Departments select allDepartments);

                // Bind results to gridview
                DepartmentsGridView.DataSource = Departments.AsQueryable().OrderBy(SortString).ToList();
                DepartmentsGridView.DataBind();
            }
        }