protected void btnSave_Click(object sender, EventArgs e) { using (DefaultConnection db = new DefaultConnection()) { Student stud = new Student(); if (!String.IsNullOrEmpty(Request.QueryString["StudentID"])) { Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); stud = (from s in db.Students where s.StudentID == StudentID select s).FirstOrDefault(); } stud.LastName = txtName.Text; stud.FirstMidName = txtFirstName.Text; stud.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text); // add the new department if no url if (String.IsNullOrEmpty(Request.QueryString["StudentID"])) { db.Students.Add(stud); db.SaveChanges(); } // save new department db.SaveChanges(); //redirect Response.Redirect("students.aspx"); } }
protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e) { // identify the department id to be deleted from the row the user selected Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]); //connect using (DefaultConnection db = new DefaultConnection()) { Student stud = (from s in db.Students where s.StudentID == StudentID select s).FirstOrDefault(); // delete db.Students.Remove(stud); db.SaveChanges(); //refresh the grid GetStudents(); } }