protected void GetCourse()
        {
            try
            {
                int courseID = int.Parse(Request.QueryString["CourseID"]);

                using (comp2007Entities db = new comp2007Entities())
                {
                    Course course = (from obj in db.Courses where obj.CourseID == courseID select obj).FirstOrDefault();

                    txtTitle.Text = course.Title;
                    txtCredits.Text = course.Credits.ToString();

                    var objS = (from s in db.Students
                                join e in db.Enrollments on s.StudentID equals e.StudentID
                                join c in db.Courses on e.CourseID equals c.CourseID
                                where c.CourseID == courseID
                                select new { s.StudentID, s.LastName, s.FirstMidName, s.EnrollmentDate, e.EnrollmentID });

                    grdCourseStudents.DataSource = objS.ToList();
                    grdCourseStudents.DataBind();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void GetDepartment()
        {
            try
            {
                int departmentID = int.Parse(Request.QueryString["DepartmentID"]);

                using (comp2007Entities db = new comp2007Entities())
                {
                    Department department = (from obj in db.Departments where obj.DepartmentID == departmentID select obj).FirstOrDefault();

                    txtName.Text = department.Name;
                    txtBudget.Text = department.Budget.ToString();

                    var objD = (from d in db.Departments
                                join c in db.Courses on d.DepartmentID equals c.DepartmentID
                                where d.DepartmentID == departmentID
                                select new { c.CourseID, c.Title, c.Credits });

                    grdDepartmentCourses.DataSource = objD.ToList();
                    grdDepartmentCourses.DataBind();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void GetStudent()
        {
            try
            {
                int studentID = int.Parse(Request.QueryString["StudentID"]);

                using (comp2007Entities db = new comp2007Entities())
                {
                    Student s = (from objS in db.Students where objS.StudentID == studentID select objS).FirstOrDefault();

                    txtLastName.Text = s.LastName;
                    txtFirstMidName.Text = s.FirstMidName;

                    txtEnrollmentDate.Text = s.EnrollmentDate.ToString("yyyy-mm-dd");

                    var objE = (from en in db.Enrollments
                                join c in db.Courses on en.CourseID equals c.CourseID
                                join d in db.Departments on c.DepartmentID equals d.DepartmentID
                                where en.StudentID == studentID
                                select new { en.EnrollmentID, en.Grade, c.Title, d.Name });

                    grdStudentCourses.DataSource = objE.ToList();
                    grdStudentCourses.DataBind();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void GetStudents()
        {
            try
            {
                using (comp2007Entities db = new comp2007Entities())
                {
                    var Students = from s in db.Students
                                   select s;

                    grdStudents.DataSource = Students.ToList();
                    grdStudents.DataBind();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void GetDepartments()
        {
            try
            {
                using (comp2007Entities db = new comp2007Entities())
                {
                    var departments = from d in db.Departments
                                      select d;

                    grdDepartments.DataSource = departments.ToList();
                    grdDepartments.DataBind();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void GetCourses()
        {
            try
            {
                using (comp2007Entities db = new comp2007Entities())
                {
                    string SortString = Session["SortColumn"].ToString();

                    var courses = from c in db.Courses
                                  select c;

                    grdCourses.DataSource = courses.AsQueryable().OrderBy(SortString).ToList();
                    grdCourses.DataBind();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //use EF to connect to SQL server
                using (comp2007Entities db = new comp2007Entities())
                {
                    //use the Student model to save the new record
                    Course c = new Course();
                    int courseID = 0;

                    if (Request.QueryString["CourseID"] != null)
                    {
                        courseID = int.Parse(Request.QueryString["CourseID"]);

                        c = (from obj in db.Courses where obj.CourseID == courseID select obj).FirstOrDefault();
                    }

                    c.Title = txtTitle.Text;
                    c.Credits = int.Parse(txtCredits.Text);
                    c.DepartmentID = int.Parse(ddlDepartment.SelectedValue);

                    if (courseID == 0)
                    {
                        db.Courses.Add(c);
                    }

                    db.SaveChanges();

                    //redirect to the updated students page
                    Response.Redirect("courses.aspx");
                }

            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //use EF to connect to SQL server
                using (comp2007Entities db = new comp2007Entities())
                {
                    //use the Student model to save the new record
                    Student s = new Student();
                    int studentID = 0;

                    if (Request.QueryString["StudentID"] != null)
                    {
                        studentID = int.Parse(Request.QueryString["StudentID"]);

                        s = (from objS in db.Students where objS.StudentID == studentID select objS).FirstOrDefault();
                    }

                    s.LastName = txtLastName.Text;
                    s.FirstMidName = txtFirstMidName.Text;
                    s.EnrollmentDate = DateTime.Parse(txtEnrollmentDate.Text);

                    if (studentID == 0)
                    {
                        db.Students.Add(s);
                    }

                    db.SaveChanges();

                    //redirect to the updated students page
                    Response.Redirect("students.aspx");
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //use EF to connect to SQL server
                using (comp2007Entities db = new comp2007Entities())
                {
                    //use the Student model to save the new record
                    Department d = new Department();
                    int departmentID = 0;

                    if (Request.QueryString["DepartmentID"] != null)
                    {
                        departmentID = int.Parse(Request.QueryString["DepartmentID"]);

                        d = (from obj in db.Departments where obj.DepartmentID == departmentID select obj).FirstOrDefault();
                    }

                    d.Name = txtName.Text;
                    d.Budget = decimal.Parse(txtBudget.Text);

                    if (departmentID == 0)
                    {
                        db.Departments.Add(d);
                    }

                    db.SaveChanges();

                    //redirect to the updated students page
                    Response.Redirect("departments.aspx");
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void grdCourseStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int enrollmentID = Convert.ToInt32(grdCourseStudents.DataKeys[e.RowIndex].Values["EnrollmentID"]);

                using (comp2007Entities db = new comp2007Entities())
                {
                    var objE = (from en in db.Enrollments
                                where en.EnrollmentID == enrollmentID
                                select en).FirstOrDefault();

                    db.Enrollments.Remove(objE);
                    db.SaveChanges();

                    GetCourse();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                using (comp2007Entities db = new comp2007Entities())
                {
                    var depts = (from obj in db.Departments select obj);

                    ddlDepartment.DataSource = depts.ToList();
                    ddlDepartment.DataTextField = "Name";
                    ddlDepartment.DataValueField = "DepartmentID";
                    ddlDepartment.DataBind();
                }

                if (!IsPostBack && Request.QueryString.Count > 0)
                {
                    GetCourse();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int selectedRow = e.RowIndex;

                int courseID = (int)grdCourses.DataKeys[selectedRow].Values["CourseID"];

                using (comp2007Entities db = new comp2007Entities())
                {
                    Course c = (from obj in db.Courses where obj.CourseID == courseID select obj).FirstOrDefault();

                    db.Courses.Remove(c);
                    db.SaveChanges();

                    GetCourses();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int selectedRow = e.RowIndex;

                int studentID = (int)grdStudents.DataKeys[selectedRow].Values["StudentID"];

                using (comp2007Entities db = new comp2007Entities())
                {
                    Student s = (from objS in db.Students where objS.StudentID == studentID select objS).FirstOrDefault();

                    db.Students.Remove(s);
                    db.SaveChanges();

                    GetStudents();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/error.aspx");
            }
        }