protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {

                    Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);
                    Int32 CourseID = Convert.ToInt32(ddlCourse.SelectedValue);

                    Enrollment objE = new Enrollment();

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

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

                    //refresh
                    GetStudents();

                }
            }
            catch (Exception exc)
            {
                Response.Redirect("~/error.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //instantiate a new student object in memory
                    Student s = new Student();

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

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

                    //fill the properties of our object from the form inputs
                    s.FirstMidName = txtFirstName.Text;
                    s.LastName = txtLastName.Text;
                    s.EnrollmentDate = Convert.ToDateTime(txtEnrollDate.Text);

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

                    //redirect to updated departments page
                    Response.Redirect("students.aspx");
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("~/error.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //instantiate a new student object in memory
                    Course c = new Course();

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

                        c = (from crs in conn.Courses
                             where crs.CourseID == CourseID
                             select crs).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(ddlDepartment.SelectedValue);

                    if (Request.QueryString.Count == 0)
                    {
                        conn.Courses.Add(c);
                    }
                    conn.SaveChanges();

                    //redirect to updated departments page
                    Response.Redirect("Courses.aspx");
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("~/error.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //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");
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("~/error.aspx");
            }
        }
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get the selected enrollment id
            Int32 EnrollmentID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["EnrollmentID"]);

            try
            {
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {

                    Enrollment objE = (from en in conn.Enrollments
                                       where en.EnrollmentID == EnrollmentID
                                       select en).FirstOrDefault();

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

                    GetStudents();

                }
            }
            catch (Exception exception)
            {
                Response.Redirect("~/error.aspx");
            }
        }
        protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //get the selected department id
                    Int32 DepartmentID = Convert.ToInt32(grdDepartments.DataKeys[e.RowIndex].Values["DepartmentID"]);

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

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

                    //update the grid
                    GetDepartments();

                }
            }
            catch (Exception ex)
            {
                Response.Redirect("~/error.aspx");
            }
        }
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                //connect
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {
                    //get the selected department id
                    Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"]);

                    var c = (from crs in conn.Courses
                             where crs.CourseID == CourseID
                             select crs).FirstOrDefault();

                    //delete
                    conn.Courses.Remove(c);
                    conn.SaveChanges();

                    //update the grid
                    GetCourses();

                }
            }
            catch (Exception ex)
            {
                Response.Redirect("~/error.aspx");
            }
        }
        protected void grdDepCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get the selected Course id
            Int32 CourseID = Convert.ToInt32(grdDepCourses.DataKeys[e.RowIndex].Values["CourseID"]);

            try
            {
                using (DefaultConnectionEF conn = new DefaultConnectionEF())
                {

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

                    conn.Courses.Remove(objC);
                    conn.SaveChanges();

                    GetDepartment();

                }
            }
            catch (Exception exception)
            {
                Response.Redirect("~/error.aspx");
            }
        }