protected void GetCourse() { //populate the existing course for editing 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(); var objS = (from s in db.Students join en in db.Enrollments on s.StudentID equals en.StudentID join c in db.Courses on en.CourseID equals c.CourseID where c.CourseID == CourseID select new { s.StudentID, s.LastName, s.FirstMidName, s.EnrollmentDate }); grdStudents.DataSource = objS.ToList(); grdStudents.DataBind(); } }
protected void GetStudent() { Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); using (comp2007Entities db = new comp2007Entities()) { Student s = (from ObjS in db.Students where ObjS.StudentID == StudentID select ObjS).FirstOrDefault(); //map the student properties to form controls if (s != null) { txtLastName.Text = s.LastName; txtFirstName.Text = s.FirstMidName; txtEnrollmentDate.Text = s.EnrollmentDate.ToString("yyyy-MM-dd"); pnlCourses.Visible = true; } 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 == s.StudentID select new { en.EnrollmentID, d.Name, c.Title, en.Grade }); grdStudent.DataSource = objE.ToList(); grdStudent.DataBind(); } }
protected void btnSave_Click(object sender, EventArgs e) { using (comp2007Entities db = new comp2007Entities()) { //use the student model to save record Department d = new Department(); Int32 DepartmentID = 0; //check query string for an id so we can determine add or update if (Request.QueryString["DepartmentID"] != null) { DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //get the current student from EF d = (from objD in db.Departments where objD.DepartmentID == DepartmentID select objD).FirstOrDefault(); } d.Name = txtName.Text; d.Budget = Convert.ToDecimal(txtBudget.Text); //redirect to updated table of students if (DepartmentID == 0) { db.Departments.Add(d); } db.SaveChanges(); Response.Redirect("departments.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { //do insert or update 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 GetStudent() { //populate form with existing student record Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //connect to db via EF using (comp2007Entities db = new comp2007Entities()) { //populate a student instance with the StudentID from the URL parameter 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 match if (s != null) { txtLastName.Text = s.LastName; txtFirstMidName.Text = s.FirstMidName; txtEnrollmentDate.Text = s.EnrollmentDate.ToString("yyyy-MM-dd"); } 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) { using (comp2007Entities db = new comp2007Entities()) { //use the student model to save record Student s = new Student(); Enrollment en = new Enrollment(); Int32 StudentID = 0; //check query string for an id so we can determine add or update if (Request.QueryString["StudentID"] != null) { StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //get the current student from EF s = (from objS in db.Students where objS.StudentID == StudentID select objS).FirstOrDefault(); } s.LastName = txtLastName.Text; s.FirstMidName = txtFirstName.Text; s.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text); //add only if student has no ID if (StudentID == 0) { //redirect to updated table of students db.Students.Add(s); } db.SaveChanges(); Response.Redirect("students.aspx"); } }
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(); } }
protected void GetDepartments() { using (comp2007Entities db = new comp2007Entities()) { String sortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); var Departments = from d in db.Departments select d; //bind results grdDepartments.DataSource = Departments.AsQueryable().OrderBy(sortString).ToList(); grdDepartments.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 }; //bind results grdCourses.DataSource = Courses.AsQueryable().OrderBy(sortString).ToList(); grdCourses.DataBind(); } }
protected void grdStudent_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 EnrollmentID = Convert.ToInt32(grdStudent.DataKeys[e.RowIndex].Values["EnrollmentID"]); using (comp2007Entities db = new comp2007Entities()) { Enrollment objE = (from en in db.Enrollments where en.EnrollmentID == EnrollmentID select en).FirstOrDefault(); db.Enrollments.Remove(objE); db.SaveChanges(); GetStudent(); } }
protected void grdCourse_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 CourseID = Convert.ToInt32(grdCourse.DataKeys[e.RowIndex].Values["CourseID"]); using (comp2007Entities db = new comp2007Entities()) { Course objE = (from en in db.Courses where en.CourseID == CourseID select en).FirstOrDefault(); db.Courses.Remove(objE); db.SaveChanges(); GetDepartment(); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { //store the row clicked Int32 selectedRow = e.RowIndex; //get the selected StudentID Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[selectedRow].Values["CourseID"]); //using EF to remove selected student using (comp2007Entities db = new comp2007Entities()) { Course c = (from objC in db.Courses where objC.CourseID == CourseID select objC).FirstOrDefault(); db.Courses.Remove(c); db.SaveChanges(); } //refresh grid GetCourses(); }
protected void GetCourses() { //connect to EF using (comp2007Entities db = new comp2007Entities()) { //query the courses table using EF and LINQ var Course = from c in db.Courses select new {c.CourseID, c.Title, c.Credits, c.Department.Name}; //apend the current direction to the sort column String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //bind the result to the gridview grdCourses.DataSource = Course.AsQueryable().OrderBy(SortString).ToList(); grdCourses.DataBind(); } }
protected void GetStudents() { //connect to EF using (comp2007Entities db = new comp2007Entities()) { //query the students table using EF and LINQ var Students = from s in db.Students select s; //apend the current direction to the sort column String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //bind the result to the gridview grdStudents.DataSource = Students.AsQueryable().OrderBy(SortString).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 the querystring 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 EF 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); } //run the update or insert db.SaveChanges(); //redirect to the updated students page Response.Redirect("students.aspx"); } }
protected void GetDepartment() { Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); using (comp2007Entities db = new comp2007Entities()) { Department d = (from ObjD in db.Departments where ObjD.DepartmentID == DepartmentID select ObjD).FirstOrDefault(); if (d != null) { //map the student properties to form controls txtName.Text = d.Name; txtBudget.Text = d.Budget.ToString(); pnlCourse.Visible = true; } var objE = (from c in db.Courses where c.DepartmentID == d.DepartmentID select new { c.CourseID, d.Name, c.Title, c.Credits }); grdCourse.DataSource = objE.ToList(); grdCourse.DataBind(); } }
protected void grdStudents_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 StudentID = Convert.ToInt32(grdStudents.DataKeys[selectedRow].Values["StudentID"]); //use EF 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(); } //refresh the grid GetStudents(); }
protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e) { //store the row clicked Int32 selectedRow = e.RowIndex; //get the selected StudentID Int32 DepartmentID = Convert.ToInt32(grdDepartments.DataKeys[selectedRow].Values["DepartmentID"]); //using EF to remove selected student using (comp2007Entities db = new comp2007Entities()) { Department d = (from objD in db.Departments where objD.DepartmentID == DepartmentID select objD).FirstOrDefault(); db.Departments.Remove(d); db.SaveChanges(); } //refresh grid GetDepartments(); }
protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]); using (comp2007Entities db = new comp2007Entities()) { //get selected record Student objE = (from s in db.Students where s.StudentID == StudentID select s).FirstOrDefault(); //Delete db.Students.Remove(objE); db.SaveChanges(); //Refresh the data on the page GetCourse(); } }