protected void GetDepartments()
        {
            //use EF to connect and get the list of departments
            using (DefaultConnection db = new DefaultConnection())
            {
                var deps = from d in db.Departments
                           select d;

                //bind the deps query result to the grid
                grdDepartments.DataSource = deps.ToList();
                grdDepartments.DataBind();
            }
        }
        protected void GetStudents()
        {
            //use EF to connect and get the list of students
            using (DefaultConnection db = new DefaultConnection())
            {
                var studs = from s in db.Students
                           select s;

                //bind the studs query result to our grid
                grdStudents.DataSource = studs.ToList();
                grdStudents.DataBind();
            }
        }
        protected void GetDepartment()
        {
            //look up the selected department and fill the form
            using (DefaultConnection db = new DefaultConnection())
            {
                //store the id from the url in a variable
                Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

                //look up the department
                Department dep = (from d in db.Departments
                                  where d.DepartmentID == DepartmentID
                                  select d).FirstOrDefault();

                //pre populate the form fields
                txtName.Text = dep.Name;
                txtBudget.Text = dep.Budget.ToString();
            }
        }
        protected void GetStudent()
        {
            //look up the selected student and fill the form
            using (DefaultConnection db = new DefaultConnection())
            {
                //store the id from the url in a variable
                Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                //look up the student
                Student stud = (from s in db.Students
                               where s.StudentID == StudentID
                                  select s).FirstOrDefault();

                //pre populate the form fields
                txtLastName.Text = stud.LastName;
                txtFirstMidName.Text = stud.FirstMidName;
                txtEnrollmentDate.Text = stud.EnrollmentDate.ToString("dd-MM-yyyy");
            }
        }
        protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //identify the department id to be deleted from the row the user selected
            Int32 DepartmentID = Convert.ToInt32(grdDepartments.DataKeys[e.RowIndex].Values["DepartmentID"]);

            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                Department dep = (from d in db.Departments
                                  where d.DepartmentID == DepartmentID
                                  select d).FirstOrDefault();
                //delete
                db.Departments.Remove(dep);
                db.SaveChanges();

                //refresh the grid
                GetDepartments();
            }
        }
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //identify the student id to be deleted from the row the user selected
            Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]);

            //connect
            using (DefaultConnection db = new DefaultConnection())

            {
                Student stud = (from s in db.Students
                                where s.StudentID == StudentID
                                select s).FirstOrDefault();
                //delete
                db.Students.Remove(stud);
                db.SaveChanges();

                //refresh the grid
                GetStudents();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //connect to server
            using (DefaultConnection db = new DefaultConnection())
            {
                //Create a new student_details in memory
                Student stud = new Student();

                Int32 StudentID = 0;

                //check for a url
                if (!String.IsNullOrEmpty(Request.QueryString["StudentID"]))
                {
                    //get the id from the url
                    StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                    //look up the student
                    stud = (from s in db.Students
                            where s.StudentID == StudentID
                           select s).FirstOrDefault();
                }

                //Fill the properties of the new student
                stud.LastName = txtLastName.Text;
                stud.FirstMidName = txtFirstMidName.Text;
                stud.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text);

                //add if we have no id in the url
                if (StudentID == 0)
                {
                    db.Students.Add(stud);
                }

                //save the new student
                db.SaveChanges();

                //redirect to the students list page
                Response.Redirect("students.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //connect to server
            using (DefaultConnection db = new DefaultConnection())
            {
                //Create a new department_details in memory
                Department dep = new Department();

                Int32 DepartmentID = 0;

                //check for a url
                if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"]))
                {
                    //get the id from the url
                    DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

                    //look up the department
                    dep = (from d in db.Departments
                           where d.DepartmentID == DepartmentID
                           select d).FirstOrDefault();
                }

                //Fill the properties of the new department
                dep.Name = txtName.Text;
                dep.Budget = Convert.ToDecimal(txtBudget.Text);

                //add if we have no id in the url
                if (DepartmentID == 0)
                {
                    db.Departments.Add(dep);
                }

                //save the new department
                db.SaveChanges();

                //redirect to the departments list page
                Response.Redirect("departments.aspx");
            }
        }