protected void CourseDetails_RowDeleting(object sender, GridViewDeleteEventArgs e) { // Store Which row need to be deleted int selectedRow = e.RowIndex; // get the seleceted Enrollment Id int EnrollmentID = Convert.ToInt32(CourseDetails.DataKeys[selectedRow].Values["EnrollmentID"]); using (ControlsoContext db = new ControlsoContext()) { var deleteStudent = (from delStudent in db.Enrollments where delStudent.EnrollmentID == EnrollmentID select delStudent); // Remove Student from Enrolmented course foreach (var item in deleteStudent) { db.Enrollments.Remove(item); } db.SaveChanges(); // Refresh the Grid this.GetStudents(); } }
protected void SaveButton_Click(object sender, EventArgs e) { //use EF to conect to the server using (ControlsoContext db = new ControlsoContext()) { //use the student model to create a new students object and // save a new record Enrollment addStudent = new Enrollment(); int EnrollmentID = 0; if (Request.QueryString.Count > 0)//our URL has a StudentID in it { // get the id from the URL /*StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); * //get the Current student from EF bd * newStudent = (from student in db.Students * where student.StudentID == StudentID * select student).FirstOrDefault();*/ } // add form data th the new student record addStudent.CourseID = Convert.ToInt32(CourseIDTextBox.Text); addStudent.StudentID = Convert.ToInt32(StudentIDTextBox.Text); addStudent.Grade = Convert.ToInt32(GradeTextBox.Text); //use LINQ to ADO.NET to add / insert students into the db if (EnrollmentID == 0) { db.Enrollments.Add(addStudent); } //save our changes -also updates and inserts db.SaveChanges(); // Redirect back to the update Students page Response.Redirect("Default.aspx"); } }