Exemple #1
0
        protected void GetCourse()
        {
            //populate form with existing student record
            Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);

            //connect to db via EF
            using (comp2007Entities db = new comp2007Entities())
            {
                //populate a Course instance with the CourseID from the URL parameter
                Course c = (from objC in db.Courses
                            where objC.CourseID == CourseID
                            select objC).FirstOrDefault();

                //map the student properties to the form controls if we found a match
                if (c != null)
                {
                    txtCourseTitle.Text = c.Title;
                    txtCredits.Text = c.Credits.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 en in db.Enrollments
                            join cr in db.Courses on en.CourseID equals cr.CourseID
                            join d in db.Departments on cr.DepartmentID equals d.DepartmentID
                            join s in db.Students on en.StudentID equals s.StudentID
                            select new { en.EnrollmentID, s.LastName, s.FirstMidName, c.Title, d.Name });

                grdStudents.DataSource = objE.ToList();
                grdStudents.DataBind();
            }
        }
Exemple #2
0
        protected void GetDepartment()
        {
            // populate form
            Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);
            //Connect
            using (comp2007Entities db = new comp2007Entities())
            {
                Department d = (from objd in db.Departments
                                where objd.DepartmentID == DepartmentID
                                select objd).FirstOrDefault();

                //Map student to controls
                if (d != null)
                {
                    txtDeptName.Text = d.Name;
                    txtBudget.Text = d.Budget.ToString();
                }

                //Courses - this code goes in the same method that populates
                //the student form but below the existing code that's already in GetDepartment()
                var objC = (from c in db.Courses
                            select new { c.CourseID, c.Title, c.Department.Name });

                grdcourses.DataSource = objC.ToList();
                grdcourses.DataBind();

            }
        }
Exemple #3
0
        protected void GetCourses()
        {
            using (comp2007Entities db = new comp2007Entities())
            {
                String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();

                var Courses = from c in db.Courses
                              select new { c.CourseID, c.Title, c.Credits, c.Department.Name };

                grdCourses.DataSource = Courses.AsQueryable().OrderBy(SortString).ToList();
                grdCourses.DataBind();
            }
        }
Exemple #4
0
        protected void GetStudents()
        {
            //connect to EF
            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 the result to the gridview
                grdStudents.DataSource = Students.AsQueryable().OrderBy(SortString).ToList();
                grdStudents.DataBind();

            }
        }
Exemple #5
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //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();

                d.Name = txtDeptName.Text;
                d.Budget = Convert.ToDecimal(txtBudget.Text);

                db.Departments.Add(d);
                db.SaveChanges();

                //redirect to the updated students page
                Response.Redirect("departments.aspx");
            }
        }
Exemple #6
0
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get selected course ID
            Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"]);

            using (comp2007Entities db = new comp2007Entities())
            {
                //get selected course
                Course objC = (from c in db.Courses
                               where c.CourseID == CourseID
                               select c).FirstOrDefault();

                //delete
                db.Courses.Remove(objC);
                db.SaveChanges();

                //refresh grid
                GetCourses();
            }
        }
Exemple #7
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //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 querystring 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 EF
                    c = (from objS in db.Courses
                         where objS.CourseID == CourseID
                         select objS).FirstOrDefault();
                }

                c.Title = txtCourseTitle.Text;
                c.Credits = Convert.ToInt32(txtCredits.Text);

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

                //run the update or insert
                db.SaveChanges();

                //redirect to the updated students page
                Response.Redirect("courses.aspx");
            }
        }
Exemple #8
0
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //get selected record id
            Int32 EnrollmentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["EnrollmentID"]);

            using (comp2007Entities db = new comp2007Entities())
            {
                //get selected record
                Enrollment objE = (from en in db.Enrollments
                                   where en.EnrollmentID == EnrollmentID
                                   select en).FirstOrDefault();

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

                //refresh the data on the page
                GetCourse();
            }
        }
Exemple #9
0
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //store which row was clicked
            Int32 selectedRow = e.RowIndex;

            //get the select student ID using the grids data key collection
            Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[selectedRow].Values["StudentID"]);

            //connect to EF to remove student from db
            using (comp2007Entities db = new comp2007Entities())
            {
                Student s = (from objs in db.Students
                             where objs.StudentID == StudentID
                             select objs).FirstOrDefault();

                //Delete
                db.Students.Remove(s);
                db.SaveChanges();
            }

            // refresh the grid
            GetStudents();
        }