protected void TodoGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { // store which row was clicked int selectedRow = e.RowIndex; // get the selected todoID using the Grid's DataKey collection int TodoID = Convert.ToInt32(TodoListGridView.DataKeys[selectedRow].Values["TodoID"]); // use EF and LINQ to find the selected todo in the DB and remove it using (TodoConetxt db = new TodoConetxt()) { // create object to the todo clas and store the query inside of it Toodo deleteTodo = (from TodoRecords in db.Todoes where TodoRecords.TodoID == TodoID select TodoRecords).FirstOrDefault(); // remove the selected todo from the db db.Todoes.Remove(deleteTodo); // save my changes back to the db db.SaveChanges(); // refresh the grid this.TodoData(); } }
/// <summary> /// This method gets the Todo data from the DB /// </summary> private void TodoData() { // connect to DB using (TodoConetxt db = new TodoConetxt()) { // query the Student Table using EF and LINQ var ToDo = (from allTodos in db.Todoes select allTodos); // bind the result to the Students GridView TodoListGridView.DataSource = ToDo.ToList(); TodoListGridView.DataBind(); } }
protected void SaveButton_Click(object sender, EventArgs e) { // Use EF to conect to the server using (TodoConetxt db = new TodoConetxt()) { // use the student model to create a new student object and // save a new record TodoList newTodo = new TodoList(); int TodoID = 0; if (Request.QueryString.Count > 0) // our URL has a STUDENTID in it { // get the id from the URL } // add form data to the new student record newTodo.Todo Name = TodoNameTextBox.Text; newTodo.Todo Notes = TodoNotesTextBox.Text; // use LINQ to ADO.NET to add / insert new student into the db if (TodoID == 0) { db.TodoList.Add(newTodo); } // save our changes - also updates and inserts db.SaveChanges(); // Redirect back to the updated students page Response.Redirect("~/Students.aspx"); } }