protected void ButtonDeleteCategory_Click(object sender, EventArgs e) { using (var context = new TodoListsEntities()) { var categoryToRemove = context.Categories.Find(int.Parse(this.ListBoxCategories.SelectedValue)); context.TodoLists.RemoveRange(categoryToRemove.TodoLists); context.Categories.Remove(categoryToRemove); context.SaveChanges(); Response.Redirect(Request.RawUrl); } }
private void CreateOrUpdateTodoList(bool create = false) { using (var context = new TodoListsEntities()) { var category = context.Categories.Find(int.Parse(ListBoxCategories.SelectedValue)); if (create) { TodoList todoList = new TodoList(); todoList.Category = category; todoList.Title = this.TextBoxTodoTitle.Text; todoList.Text = this.TextBoxTodoText.Text; todoList.LastChangeDate = DateTime.Now; context.TodoLists.Add(todoList); context.SaveChanges(); } else { int todoListId = int.Parse(this.ButtonEditTodoList.CommandArgument); var todoToUpdate = context.TodoLists.Find(todoListId); todoToUpdate.Title = this.TextBoxEditedTitle.Text; todoToUpdate.Text = this.TextBoxEditedText.Text; todoToUpdate.LastChangeDate = DateTime.Now; context.SaveChanges(); } this.ListViewTodoLists.DataBind(); } }
private void CreateOrUpdateCategory(bool create = true) { string categoryName = this.TextBoxCategoryName.Text; if (string.IsNullOrEmpty(categoryName)) { return; } using (var context = new TodoListsEntities()) { if (create) { context.Categories.Add(new Category() { Name = categoryName }); context.SaveChanges(); } else { var existingCategory = context.Categories.Find(int.Parse(this.ListBoxCategories.SelectedValue)); existingCategory.Name = this.TextBoxCategoryName.Text; context.SaveChanges(); } } this.ListBoxCategories.DataBind(); this.PanelInsertCategory.Visible = false; }