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(); 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"); } }
protected void GetStudent() { //populate form with existing student record Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); //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(); } }
protected void btnSave_Click(object sender, EventArgs e) { // use EF to connect to SQL using (comp2007Entities db = new comp2007Entities()) { //use the student model to save the new model Department d = new Department(); Int32 DepartmentID = 0; //check save? or update?? if (Request.QueryString["DepartmentID"] != null) { //get the id from the url DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //get the current student from EF d = (from objS in db.Departments where objS.DepartmentID == DepartmentID select objS).FirstOrDefault(); } d.Name = txtDeptName.Text; d.Budget = Convert.ToDecimal(txtBudget.Text); if (DepartmentID == 0) { db.Departments.Add(d); } db.SaveChanges(); //redirect to the updated list page Response.Redirect("departments.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"]); //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(); } //refresh the grid GetDepartment(); }
protected void getDepartment() { //populate form with existing student record Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //connect to db via EF using (comp2007Entities db = new comp2007Entities()) { //populate a student instance with the student ID from the url parameter Department d = (from objS in db.Departments where objS.DepartmentID == DepartmentID select objS).FirstOrDefault(); //map the student to the form controls when s found if (d != null) { txtDeptName.Text = d.Name; txtBudget.Text = Convert.ToString(d.Budget); } //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 where c.DepartmentID == DepartmentID select new { c.CourseID, c.Title, c.Credits}); 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()) { //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"); } }
protected void GetCourse() { //populate form with existing student record Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]); //connect to db using entity framework using (comp2007Entities db = new comp2007Entities()) { //populate a student instance with the studentID from the URL paramater Course c = (from objS in db.Courses where objS.CourseID == CourseID select objS).FirstOrDefault(); //map the student properties to the form controls if we found a record txtTitle.Text = c.Title; txtCredits.Text = c.Credits.ToString(); ddDepartment.SelectedValue = c.DepartmentID.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 s in db.Students join e in db.Enrollments on s.StudentID equals e.StudentID join co in db.Courses on e.CourseID equals co.CourseID where co.CourseID == CourseID select new { s.LastName, s.FirstMidName, s.EnrollmentDate, co.CourseID, e.EnrollmentID, s.StudentID}); grdStudent.DataSource = objE.ToList(); grdStudent.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 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"); } }
protected void GetDepartment() { //populate form with existing student record Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //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(); } }
protected void getDepartments() { using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); var departments = from objS in db.Departments select objS; //bind the result to the gridview grdDepartments.DataSource = departments.AsQueryable().OrderBy(SortString).ToList(); grdDepartments.DataBind(); } }
protected void GetStudents() { using (comp2007Entities db = new comp2007Entities()) { String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //query the students table using EF and LINQ var Students = from s in db.Students select s; //bind he result to the gridview grdStudents.DataSource = Students.AsQueryable().OrderBy(SortString).ToList(); grdStudents.DataBind(); } }
protected void GetCourses() { //connect to EF 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 new { c.CourseID, c.Title, c.Credits, c.Department.Name }; //bind the result to the gridview grdCourses.DataSource = Courses.AsQueryable().OrderBy(SortString).ToList(); grdCourses.DataBind(); } }
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"]); //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(); } //refresh the grid GetStudents(); }
protected void getDepartment() { //populate form with existing student record Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //connect to db via EF using (comp2007Entities db = new comp2007Entities()) { //populate a student instance with the student ID from the url parameter Department d = (from objS in db.Departments where objS.DepartmentID == DepartmentID select objS).FirstOrDefault(); //map the student to the form controls when s found if (d != null) { txtDeptName.Text = d.Name; txtBudget.Text = Convert.ToString(d.Budget); } } }
protected void grdDepts_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 DepartmentID = Convert.ToInt32(grdDepts.DataKeys[selectedRow].Values["DepartmentID"]); //use Entity Framework to remove the selected student from the db using (comp2007Entities db = new comp2007Entities()) { Department d = (from objS in db.Departments where objS.DepartmentID == DepartmentID select objS).FirstOrDefault(); //do the delete db.Departments.Remove(d); db.SaveChanges(); } //refresh the grid GetDepartments(); }
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"]); //use EF 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(); } //refresh the grid GetCourses(); }
protected void grdStudent_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get the selected studentID using the grid's data key collection Int32 EnrollmentID = Convert.ToInt32(grdStudent.DataKeys[e.RowIndex].Values["EnrollmentID"]); //DataKeys[e.RowIndex].Values["StudentID"]); //use Entity Framework to remove the selected student from the db using (comp2007Entities db = new comp2007Entities()) { Enrollment objE = (from er in db.Enrollments where er.EnrollmentID == EnrollmentID select er).FirstOrDefault(); //do the delete db.Enrollments.Remove(objE); db.SaveChanges(); } //refresh the grid GetCourse(); }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 CourseID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["CourseID"]); 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(); getDepartment(); } }
protected void Page_Load(object sender, EventArgs e) { //if save wasn't clicked AND we have a studentID in the URL if ((!IsPostBack) && (Request.QueryString.Count > 0)) { GetCourse(); } using (comp2007Entities db = new comp2007Entities()) { var objD = (from d in db.Departments select new { d.DepartmentID, d.Name }); ddDepartment.DataValueField = "DepartmentID"; ddDepartment.DataTextField = "Name"; ddDepartment.DataSource = objD.ToList(); ddDepartment.DataBind(); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 EnrollemntID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["EnrollmentID"]); using (comp2007Entities db = new comp2007Entities()) { Enrollment objE = (from en in db.Enrollments where en.EnrollmentID == EnrollemntID select en).FirstOrDefault(); db.Enrollments.Remove(objE); db.SaveChanges(); GetStudent(); } }