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 btnSave_Click(object sender, EventArgs e) { //connect using (DefaultConnection conn = new DefaultConnection()) { //instantiate a new student object in memory Student s = new Student(); //decide if updating or adding, then save if (Request.QueryString.Count > 0) { Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]); s = (from stu in conn.Students where stu.StudentID == StudentID select stu).FirstOrDefault(); } //fill the properties of our object from the form inputs s.FirstMidName = txtFirstName.Text; s.LastName = txtLastName.Text; s.EnrollmentDate = Convert.ToDateTime(txtEnrollDate.Text); if (Request.QueryString.Count == 0) { conn.Students.Add(s); } conn.SaveChanges(); //redirect to updated departments page Response.Redirect("students.aspx"); } }