protected void GetDepartment()
        {
            //populate form with existing student record
            Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

            try
            {
                //connect to db using entity framework
                using (comp2007Entities db = new comp2007Entities())
                {
                    //populate a student instance with the studentID from the URL paramater
                    Department d = (from objS in db.Departments
                                    where objS.DepartmentID == DepartmentID
                                    select objS).FirstOrDefault();

                    //map the student properties to the form controls if we found a record

                    txtDeptName.Text = d.Name;
                    txtBudget.Text = d.Budget.ToString();

                    //enrollments - this code goes in the same method that populates the student form but below the existing code that's already in GetStudent()
                    var objE = (from c in db.Courses
                                join de in db.Departments on c.DepartmentID equals DepartmentID
                                where c.DepartmentID == DepartmentID
                                select new { c.CourseID, c.Title, c.Credits });

                    grdDepartment.DataSource = objE.ToList();
                    grdDepartment.DataBind();
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            using (comp2007Entities db = new comp2007Entities())
            {
                Enrollment objE = new Enrollment();
                objE.StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);
                objE.CourseID = Convert.ToInt32(ddlCourse.SelectedValue);

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

                //add to the enrollments table
                GetEnrollments();

            }
        }
        protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 DepartmentID = Convert.ToInt32(ddlDepartment.SelectedIndex);

            using(comp2007Entities db = new comp2007Entities())
            {
                var objC = from c in db.Courses
                           where c.DepartmentID == DepartmentID
                           orderby c.Title
                           select c;

                //populate course dropdown
                ddlCourse.DataSource = objC.ToList();
                ddlCourse.DataBind();

                ListItem newItem = new ListItem("-SELECT-", "0");
                ddlCourse.Items.Insert(0, newItem);
            }
        }
        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();
                    Int32 CourseID = 0;
                    //check the query string for an id so we can determine add / update
                    if (Request.QueryString["CourseID"] != null)
                    {
                        //get the ID from the URL
                        CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);

                        //get the current student from Entity Framework
                        c = (from objS in db.Courses
                             where objS.CourseID == CourseID
                             select objS).FirstOrDefault();
                    }
                    c.Title = txtTitle.Text;
                    c.Credits = Convert.ToInt32(txtCredits.Text);
                    c.DepartmentID = Convert.ToInt32(ddDepartment.SelectedValue);

                    //call add only if we have no student ID
                    if (CourseID == 0)
                        db.Courses.Add(c);

                    db.SaveChanges();
                    //redirect to the updated students page
                    Response.Redirect("courses.aspx");
                }
            }
            catch (Exception)
            {
                Server.Transfer("/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();
                    Int32 StudentID = 0;
                    //check the query string for an id so we can determine add / update
                    if (Request.QueryString["StudentID"] != null)
                    {
                        //get the ID from the URL
                        StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                        //get the current student from Entity Framework
                        s = (from objS in db.Students
                             where objS.StudentID == StudentID
                             select objS).FirstOrDefault();
                    }
                    s.LastName = txtLastName.Text;
                    s.FirstMidName = txtFirstMidName.Text;
                    s.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text);

                    //call add only if we have no student ID
                    if (StudentID == 0)
                        db.Students.Add(s);

                    db.SaveChanges();
                    //redirect to the updated students page
                    Response.Redirect("students.aspx");
                }
            }
            catch (Exception)
            {
                Server.Transfer("/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();
                    Int32 DepartmentID = 0;
                    //check the query string for an id so we can determine add / update
                    if (Request.QueryString["DepartmentID"] != null)
                    {
                        //get the ID from the URL
                        DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

                        //get the current student from Entity Framework
                        d = (from objS in db.Departments
                             where objS.DepartmentID == DepartmentID
                             select objS).FirstOrDefault();
                    }
                    d.Name = txtDeptName.Text;
                    d.Budget = Convert.ToDecimal(txtBudget.Text);

                    //call add only if we have no student ID
                    if (DepartmentID == 0)
                        db.Departments.Add(d);

                    db.SaveChanges();
                    //redirect to the updated students page
                    Response.Redirect("departments.aspx");
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
        protected void GetStudents()
        {
            try
            {
                using (comp2007Entities db = new comp2007Entities())
                {
                    String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();

                    //query the students table using EF and LINQ
                    var Students = from s in db.Students
                                   select s;

                    //bind he result to the gridview
                    grdStudents.DataSource = Students.AsQueryable().OrderBy(SortString).ToList();
                    grdStudents.DataBind();

                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
        protected void GetCourse()
        {
            //populate form with existing student record
            Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);
            try
            {
                //connect to db using entity framework
                using (comp2007Entities db = new comp2007Entities())
                {
                    //populate a student instance with the studentID from the URL paramater
                    Course c = (from objS in db.Courses
                                where objS.CourseID == CourseID
                                select objS).FirstOrDefault();

                    //map the student properties to the form controls if we found a record

                    txtTitle.Text = c.Title;
                    txtCredits.Text = c.Credits.ToString();
                    ddDepartment.SelectedValue = c.DepartmentID.ToString();

                    //enrollments - this code goes in the same method that populates the student form but below the existing code that's already in GetStudent()
                    var objE = (from s in db.Students
                                join e in db.Enrollments on s.StudentID equals e.StudentID
                                join co in db.Courses on e.CourseID equals co.CourseID
                                where co.CourseID == CourseID
                                select new { s.LastName, s.FirstMidName, s.EnrollmentDate, co.CourseID, e.EnrollmentID, s.StudentID });

                    grdStudent.DataSource = objE.ToList();
                    grdStudent.DataBind();

                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get the selected studentID using the grid's data key collection
            Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]);

            try
            {
                //use Entity Framework to remove the selected student from the db
                using (comp2007Entities db = new comp2007Entities())
                {
                    Student s = (from objS in db.Students
                                 where objS.StudentID == StudentID
                                 select objS).FirstOrDefault();

                    //do the delete
                    db.Students.Remove(s);
                    db.SaveChanges();
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }

            //refresh the grid
            GetStudents();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //if save wasn't clicked AND we have a studentID in the URL
            if ((!IsPostBack) && (Request.QueryString.Count > 0))
            {
                GetCourse();
            }
            try
            {
                using (comp2007Entities db = new comp2007Entities())
                {
                    var objD = (from d in db.Departments
                                select new { d.DepartmentID, d.Name });

                    ddDepartment.DataValueField = "DepartmentID";
                    ddDepartment.DataTextField = "Name";
                    ddDepartment.DataSource = objD.ToList();
                    ddDepartment.DataBind();
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
        protected void grdDepartment_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get the selected studentID using the grid's data key collection
            Int32 CourseID = Convert.ToInt32(grdDepartment.DataKeys[e.RowIndex].Values["CourseID"]);

            try
            {
                //use Entity Framework to remove the selected student from the db
                using (comp2007Entities db = new comp2007Entities())
                {
                    var objE = from en in db.Enrollments
                               where en.CourseID == CourseID
                               select en;

                    //do the delete
                    foreach (var enrollments in objE)
                    {
                        db.Enrollments.Remove(enrollments);
                    }

                    db.SaveChanges();

                    Course objC = (from c in db.Courses
                                   where c.CourseID == CourseID
                                   select c).FirstOrDefault();

                    //do the delete
                    db.Courses.Remove(objC);
                    db.SaveChanges();
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }

            //refresh the grid
            GetDepartment();
        }
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //store which row was clicked
            Int32 selectedRow = e.RowIndex;

            //get the selected studentID using the grid's data key collection
            Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[selectedRow].Values["CourseID"]);

            try
            {
                //use Entity Framework to remove the selected student from the db
                using (comp2007Entities db = new comp2007Entities())
                {
                    Course c = (from objS in db.Courses
                                where objS.CourseID == CourseID
                                select objS).FirstOrDefault();

                    //do the delete
                    db.Courses.Remove(c);
                    db.SaveChanges();
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }

            //refresh the grid
            GetCourses();
        }
        protected void GetDepartments()
        {
            try
            {
                using (comp2007Entities db = new comp2007Entities())
                {

                    //query the students table using EF and LINQ
                    var Dept = from d in db.Departments
                               orderby d.Name
                               select d;

                    ddlDepartment.DataSource = Dept.ToList();
                    ddlDepartment.DataBind();

                    ListItem newItem = new ListItem("-Select-", "0");
                    ddlDepartment.Items.Insert(0, newItem);
                    ddlCourse.Items.Insert(0, newItem);

                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
        protected void GetStudent()
        {
            //populate form with existing student record
            Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

            try
            {
                //connect to db using entity framework
                using (comp2007Entities db = new comp2007Entities())
                {
                    //populate a student instance with the studentID from the URL paramater
                    Student s = (from objS in db.Students
                                 where objS.StudentID == StudentID
                                 select objS).FirstOrDefault();

                    //map the student properties to the form controls if we found a record
                    if (s != null)
                    {
                        txtLastName.Text = s.LastName;
                        txtFirstMidName.Text = s.FirstMidName;
                        txtEnrollmentDate.Text = s.EnrollmentDate.ToString("yyyy-MM-dd");
                    }
                    //enrollments - this code goes in the same method that populates the student form but below the existing code that's already in GetStudent()
                    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 });

                    grdCourses.DataSource = objE.ToList();
                    grdCourses.DataBind();
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
        protected void GetEnrollments()
        {
            int StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

            using (comp2007Entities db = new comp2007Entities())
            {
                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 });

                grdCourses.DataSource = objE.ToList();
                grdCourses.DataBind();

            }
        }