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 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 GetDepartment(); } }
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(); }