protected void GetStudent() { // populate form Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //Connect using (comp2007Entities db = new comp2007Entities()) { Student s = (from objs in db.Students where objs.StudentID == StudentID select objs).FirstOrDefault(); //Map student to controls 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(); } }
protected void btnSave_Click(object sender, EventArgs e) { //use EF to connect to SQL Server using (comp2007Entities db = new comp2007Entities()) { Course objC = new Course(); if (!String.IsNullOrEmpty(Request.QueryString["CourseID"])) { Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); objC = (from c in db.Courses where c.CourseID == CourseID select c).FirstOrDefault(); } //populate the course from the input form objC.Title = txtTitle.Text; objC.Credits = Convert.ToInt32(txtCredits.Text); objC.DepartmentID = Convert.ToInt32(ddlDepartment.SelectedValue); if (String.IsNullOrEmpty(Request.QueryString["CourseID"])) { //add db.Courses.Add(objC); } //save and redirect db.SaveChanges(); Response.Redirect("courses.aspx"); } }
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(); } }
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(); } }
protected void GetCourse() { //connect to db via EF using (comp2007Entities db = new comp2007Entities()) { Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); Course objC = (from c in db.Courses where c.CourseID == CourseID select c).FirstOrDefault(); //populate the form txtTitle.Text = objC.Title; txtCredits.Text = objC.Credits.ToString(); ddlDepartment.SelectedValue = objC.DepartmentID.ToString(); } }
protected void GetDepartments() { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //connect to EF using (comp2007Entities db = new comp2007Entities()) { //query the students table using EF and LINQ var Departments = from d in db.Departments select d; //bind the result to the gridview grdDepartments.DataSource = Departments.AsQueryable().OrderBy(SortString).ToList(); grdDepartments.DataBind(); } }
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"); } }
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(); } }
protected void GetDepartments() { using (comp2007Entities db = new comp2007Entities()) { var deps = (from d in db.Departments orderby d.Name select d); ddlDepartment.DataSource = deps.ToList(); ddlDepartment.DataBind(); //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, cr.Title, d.Name }); grdStudents.DataSource = objE.ToList(); grdStudents.DataBind(); } }
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 Student s = new Student(); Int32 StudentID = 0; //check query string if (Request.QueryString["StudentID"] != null) { //Get url StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //Get Student 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); if (StudentID == 0) { db.Students.Add(s); } db.SaveChanges(); //redirect to the updated students page Response.Redirect("students.aspx"); } }
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(); } }
protected void grdDepartments_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 DepartmentID = Convert.ToInt32(grdDepartments.DataKeys[selectedRow].Values["DepartmentID"]); //connect to EF to remove student from db using (comp2007Entities db = new comp2007Entities()) { Department d = (from objs in db.Departments where objs.DepartmentID == DepartmentID select objs).FirstOrDefault(); //Delete db.Departments.Remove(d); db.SaveChanges(); } // refresh the grid GetDepartments(); }