示例#1
0
        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();

            }
        }
示例#2
0
        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");
            }
        }
示例#3
0
        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 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 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();
        }
示例#7
0
        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();
        }