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 Course c = new Course(); int courseID = 0; if (Request.QueryString["CourseID"] != null) { courseID = int.Parse(Request.QueryString["CourseID"]); c = (from obj in db.Courses where obj.CourseID == courseID select obj).FirstOrDefault(); } c.Title = txtTitle.Text; c.Credits = int.Parse(txtCredits.Text); c.DepartmentID = int.Parse(ddlDepartment.SelectedValue); if (courseID == 0) { db.Courses.Add(c); } db.SaveChanges(); //redirect to the updated students page Response.Redirect("courses.aspx"); } } catch (Exception ex) { Response.Redirect("/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(); int studentID = 0; if (Request.QueryString["StudentID"] != null) { studentID = int.Parse(Request.QueryString["StudentID"]); s = (from objS in db.Students where objS.StudentID == studentID select objS).FirstOrDefault(); } s.LastName = txtLastName.Text; s.FirstMidName = txtFirstMidName.Text; s.EnrollmentDate = DateTime.Parse(txtEnrollmentDate.Text); if (studentID == 0) { db.Students.Add(s); } db.SaveChanges(); //redirect to the updated students page Response.Redirect("students.aspx"); } } catch (Exception ex) { Response.Redirect("/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(); int departmentID = 0; if (Request.QueryString["DepartmentID"] != null) { departmentID = int.Parse(Request.QueryString["DepartmentID"]); d = (from obj in db.Departments where obj.DepartmentID == departmentID select obj).FirstOrDefault(); } d.Name = txtName.Text; d.Budget = decimal.Parse(txtBudget.Text); if (departmentID == 0) { db.Departments.Add(d); } db.SaveChanges(); //redirect to the updated students page Response.Redirect("departments.aspx"); } } catch (Exception ex) { Response.Redirect("/error.aspx"); } }
protected void grdCourseStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { int enrollmentID = Convert.ToInt32(grdCourseStudents.DataKeys[e.RowIndex].Values["EnrollmentID"]); using (comp2007Entities db = new comp2007Entities()) { var objE = (from en in db.Enrollments where en.EnrollmentID == enrollmentID select en).FirstOrDefault(); db.Enrollments.Remove(objE); db.SaveChanges(); GetCourse(); } } catch (Exception ex) { Response.Redirect("/error.aspx"); } }
protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { int selectedRow = e.RowIndex; int courseID = (int)grdCourses.DataKeys[selectedRow].Values["CourseID"]; using (comp2007Entities db = new comp2007Entities()) { Course c = (from obj in db.Courses where obj.CourseID == courseID select obj).FirstOrDefault(); db.Courses.Remove(c); db.SaveChanges(); GetCourses(); } } catch (Exception ex) { Response.Redirect("/error.aspx"); } }
protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { int selectedRow = e.RowIndex; int studentID = (int)grdStudents.DataKeys[selectedRow].Values["StudentID"]; using (comp2007Entities db = new comp2007Entities()) { Student s = (from objS in db.Students where objS.StudentID == studentID select objS).FirstOrDefault(); db.Students.Remove(s); db.SaveChanges(); GetStudents(); } } catch (Exception ex) { Response.Redirect("/error.aspx"); } }