protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { //Store which row was clicked Int32 selectedRow = e.RowIndex; //get the selected sudent ID using the grids data key collection Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[selectedRow].Values["StudentID"]); //use entity to find the object and remove it using (GetWreckedEntities db = new GetWreckedEntities()) { Student s = (from objs in db.Students where objs.StudentID == StudentID select objs).FirstOrDefault(); //now we need to do the delete db.Students.Remove(s); db.SaveChanges(); } //refresh the grid getStudents(); } catch (System.Exception) { Response.Redirect("/Error.aspx"); } }
protected void GetStudent() { try { //populate form for edit //get the ID Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //connec tot he db using Entity using (GetWreckedEntities db = new GetWreckedEntities()) { //populate from a student instance with the student ID from the URL params Student s = (from objs in db.Students where objs.StudentID == StudentID select objs).FirstOrDefault(); //mpa the student properties from the form controls txtLastName.Text = s.LastName; txtFirstName.Text = s.FirstMidName; txtEnrollDate.Text = s.EnrollmentDate.ToShortDateString(); //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 (System.Exception) { Response.Redirect("/Error.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { try { using (GetWreckedEntities db = new GetWreckedEntities()) { Student s = new Student(); Int32 StudentID = 0; if (Request.QueryString.Count > 0) { StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //get the student from the entity ;D } s.LastName = txtLastName.Text; s.FirstMidName = txtFirstName.Text; s.EnrollmentDate = Convert.ToDateTime(txtEnrollDate.Text); if (StudentID == 0) { db.Students.Add(s); } db.SaveChanges(); } } catch (System.Exception) { Response.Redirect("/Error.aspx"); } }
protected void getStudents() { try { using (GetWreckedEntities db = new GetWreckedEntities()) { //query the students table using entity var Students = from s in db.Students select s; grdStudents.DataSource = Students.ToList(); grdStudents.DataBind(); } } catch (System.Exception) { Response.Redirect("/Error.aspx"); } }
protected void getCourses() { try { using (GetWreckedEntities db = new GetWreckedEntities()) { //query the students table using entity var Courses = from c in db.Courses select new { c.CourseID, c.Title, c.Credits, c.Department.Name }; String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); grdCourses.DataSource = Courses.AsQueryable().OrderBy(SortString).ToList(); grdCourses.DataBind(); } } catch (System.Exception) { Response.Redirect("/Error.aspx"); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { Int32 EnrollmentID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["EnrollmentID"]); using (GetWreckedEntities db = new GetWreckedEntities()) { Enrollment objE = (from en in db.Enrollments where en.EnrollmentID == EnrollmentID select en).FirstOrDefault(); db.Enrollments.Remove(objE); db.SaveChanges(); GetStudent(); } } catch (System.Exception) { Response.Redirect("/Error.aspx"); } }