protected void btnSave_Click(object sender, EventArgs e)
        {
            //connect
            using (ContosoEntities conn = new ContosoEntities())
            {
                //instantiate a new deparment object in memory
                Cours c = new Cours();

                //decide if updating or adding, then save
                if (!String.IsNullOrEmpty(Request.QueryString["CourseID"]))
                {
                    Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);

                    c = (from cour in conn.Courses
                         where cour.CourseID == CourseID
                         select cour).FirstOrDefault();
                }

                //fill the properties of our object from the form inputs
                c.Title = txtTitle.Text;
                c.Credits = Convert.ToInt32(txtCredits.Text);
                c.DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue);

                if (String.IsNullOrEmpty(Request.QueryString["CourseID"]))
                {
                    conn.Courses.Add(c);
                }
                conn.SaveChanges();

                //redirect to updated departments page
                Response.Redirect("courses.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //connect
            using (ContosoEntities conn = new ContosoEntities())
            {
                //instantiate a new deparment object in memory
                Department d = new Department();

                //decide if updating or adding, then save
                if (Request.QueryString.Count > 0)
                {
                    Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

                    d = (from dep in conn.Departments
                         where dep.DepartmentID == DepartmentID
                         select dep).FirstOrDefault();
                }

                //fill the properties of our object from the form inputs
                d.Name = txtName.Text;
                d.Budget = Convert.ToDecimal(txtBudget.Text);

                if (Request.QueryString.Count == 0)
                {
                    conn.Departments.Add(d);
                }
                conn.SaveChanges();

                //redirect to updated departments page
                Response.Redirect("departments.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //connect
            using (ContosoEntities conn = new ContosoEntities())
            {

                Student s = new Student();

                if (Request.QueryString["StudentID"] != null)
                {
                    Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                    s = (from stu in conn.Students where stu.StudentID == StudentID select stu).FirstOrDefault();
                }

                s.FirstMidName = txtFirstName.Text;
                s.LastName = txtLastName.Text;
                s.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text);

                if (Request.QueryString.Count == 0)
                {
                    conn.Students.Add(s);
                }
                conn.SaveChanges();

                Response.Redirect("students.aspx");
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            using (ContosoEntities conn = new ContosoEntities())
            {
                Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);
                Int32 StudentID = Convert.ToInt32(ddlStudent.SelectedValue);

                Enrollment objE = new Enrollment();

                objE.StudentID = StudentID;
                objE.CourseID = CourseID;

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

                GetCourse();
            }
        }
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"]);

            using (ContosoEntities db = new ContosoEntities())
            {
                Cours objE = (from c in db.Courses
                                   where c.CourseID == CourseID
                                   select c).FirstOrDefault();

                //process the deletion
                db.Courses.Remove(objE);
                db.SaveChanges();

                //repopulate the page
                GetDepartment();
            }
        }
        protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //connect
            using (ContosoEntities conn = new ContosoEntities())
            {
                //get the selected DepartmentID
                Int32 DepartmentID = Convert.ToInt32(grdDepartments.DataKeys[e.RowIndex].Values["DepartmentID"]);

                var d = (from dep in conn.Departments
                         where dep.DepartmentID == DepartmentID
                         select dep).FirstOrDefault();

                //process the delete
                conn.Departments.Remove(d);
                conn.SaveChanges();

                //update the grid
                GetDepartments();
            }
        }
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //connect
            using (ContosoEntities conn = new ContosoEntities())
            {

                Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]);

                var s = (from stu in conn.Students where stu.StudentID == StudentID select stu).FirstOrDefault();

                conn.Students.Remove(s);
                conn.SaveChanges();
                GetStudents();
            }
        }
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get the selected EnrollmentID
            Int32 EnrollmentID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["EnrollmentID"]);

            using (ContosoEntities db = new ContosoEntities())
            {
                Enrollment objE = (from en in db.Enrollments
                                   where en.EnrollmentID == EnrollmentID
                                   select en).FirstOrDefault();

                //process the deletion
                db.Enrollments.Remove(objE);
                db.SaveChanges();

                //repopulate the page
                GetStudent();
            }
        }