/**
         * <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 in the GridView
                StudentsGridView.DataSource = Students.AsQueryable().OrderBy(SortString).ToList();
                StudentsGridView.DataBind();
            }
        }
        protected void GetStudent()
        {
            // Populate the form with existing data
            int StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

            // Connect to the EF SB
            using (ContosoConnection db = new ContosoConnection())
            {
                // Populate the Student Object instance with the Student id 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)
        {
            using (ContosoConnection db = new ContosoConnection())
            {
                // use the Student model to create a new student object and
                // also save a new record
                Student newStudent = new Student();

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

                    //get the current student from the 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/ insert the new student into the db
                if (StudentID == 0)
                {
                    db.Students.Add(newStudent);
                }

                //save the changes and our updates and inserts
                db.SaveChanges();

                //Redirect back to the students page
                Response.Redirect("~/Contoso/Students.aspx");
            }
        }
        /**
         * <summary>
         * This Event Handler deletes a student form the db using EF
         * </summary>
         *
         * @methodStudentGridView_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 grids data key collection
            int StudentID = Convert.ToInt32(StudentsGridView.DataKeys[selectedRow].Values["StudentID"]);

            // use EF to find the selected student from the db then remove it
            using (ContosoConnection db = new ContosoConnection())
            {
                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();
            }
        }