protected void GetDepartment() { //populate form with existing student record Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); try { //connect to db using entity framework using (comp2007Entities db = new comp2007Entities()) { //populate a student instance with the studentID from the URL paramater Department d = (from objS in db.Departments where objS.DepartmentID == DepartmentID select objS).FirstOrDefault(); //map the student properties to the form controls if we found a record txtDeptName.Text = d.Name; txtBudget.Text = d.Budget.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 c in db.Courses join de in db.Departments on c.DepartmentID equals DepartmentID where c.DepartmentID == DepartmentID select new { c.CourseID, c.Title, c.Credits }); grdDepartment.DataSource = objE.ToList(); grdDepartment.DataBind(); } } 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 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 GetStudent() { //populate form with existing student record Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); try { //connect to db using entity framework using (comp2007Entities db = new comp2007Entities()) { //populate a student instance with the studentID from the URL paramater Student s = (from objS in db.Students where objS.StudentID == StudentID select objS).FirstOrDefault(); //map the student properties to the form controls if we found a record 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(); } } catch (Exception) { Server.Transfer("/error.aspx"); } }
protected void GetCourses() { try { using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //query the students table using EF and LINQ var courses = from c in db.Courses select c; //bind he result to the gridview grdCourses.DataSource = courses.AsQueryable().OrderBy(SortString).ToList(); grdCourses.DataBind(); } } catch (Exception) { Server.Transfer("/error.aspx"); } }
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(); }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get the selected studentID using the grid's data key collection Int32 EnrollmentID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["EnrollmentID"]); try { //use Entity Framework to remove the selected student from the db using (comp2007Entities db = new comp2007Entities()) { Enrollment objE = (from en in db.Enrollments where en.EnrollmentID == EnrollmentID select en).FirstOrDefault(); //do the delete db.Enrollments.Remove(objE); db.SaveChanges(); } //refresh the grid GetStudent(); } catch (Exception) { Server.Transfer("/error.aspx"); } }
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(); }