/// <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"); } }
/// <summary> /// This method gets the todo associeted with the ID from the URL and prepopulates the form /// </summary> protected void GetTodo() { // populate teh form with existing data from the database int TodoID = Convert.ToInt32(Request.QueryString["TodoID"]); // connect to the EF DB using (TodoConnection db = new TodoConnection()) { // populate a todo object instance with the StudentID from the URL Parameter Todo updatedTodo = (from todo in db.Todos where todo.TodoID == TodoID select todo).FirstOrDefault(); // map the student properties to the form controls if (updatedTodo != null) { TodoNameTextBox.Text = updatedTodo.TodoName; TodoNotesTextBox.Text = updatedTodo.TodoNotes; TodoCompletedCheckBox.Checked = (bool) updatedTodo.Completed; } } }