/// <summary> /// This method is the event handler for the save button. Saves/Updates a todo from DB /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void SaveButton_Click(object sender, EventArgs e) { // Use EF to connect to the server using (TodoConnection db = new TodoConnection()) { // use the Todo model to create a new todo object and // save a new record Todo newTodo = new Todo(); int TodoID = 0; if (Request.QueryString.Count > 0) // our URL has a TodoID in it { // get the id from the URL TodoID = Convert.ToInt32(Request.QueryString["TodoID"]); // get the current todo from EF DB newTodo = (from todo in db.Todos where todo.TodoID == TodoID select todo).FirstOrDefault(); } // add form data to the new todo record newTodo.TodoName = TodoNameTextBox.Text; newTodo.TodoNotes = TodoNotesTextBox.Text; newTodo.Completed = TodoCompletedCheckBox.Checked; // use LINQ to ADO.NET to add / insert new todo into the database if (TodoID == 0) { db.Todos.Add(newTodo); } // save our changes - also updates and inserts db.SaveChanges(); // Redirect back to the updated students page Response.Redirect("~/Secured/TodoList.aspx"); } }