protected void ButtonAddToDo_Click(object sender, EventArgs e) { using (var context = new ToDosDbEntities()) { string categoryName = (this.GridViewToDos.FooterRow.FindControl("TextBoxNewCategoryName") as TextBox).Text; var category = context.Categories.FirstOrDefault(c => string.Compare(c.Name, categoryName, true) == 0); if (category == null) { category = new Category { Name = categoryName }; context.Categories.Add(category); context.SaveChanges(); } context.ToDos.Add(new ToDo { Title = (this.GridViewToDos.FooterRow.FindControl("TextBoxNewTitle") as TextBox).Text, Body = (this.GridViewToDos.FooterRow.FindControl("TextBoxNewBody") as TextBox).Text, LastUpdated = DateTime.Parse((this.GridViewToDos.FooterRow.FindControl("TextBoxNewLastUpdated") as TextBox).Text), Category = category }); context.SaveChanges(); Response.Redirect(Request.RawUrl); } }
protected void ListViewTodos_ItemInserting(object sender, ListViewInsertEventArgs e) { ToDosDbEntities db = new ToDosDbEntities(); this.LabelErrorMessages.Text = string.Empty; this.LabelSuccessMessages.Text = string.Empty; try { TextBox titleTextBox = (e.Item.FindControl("TitleTextBox") as TextBox); this.ValidateTitleTextBox(titleTextBox); TextBox bodyTextBox = (e.Item.FindControl("BodyTextBox") as TextBox); this.ValidateBodyTextBox(titleTextBox); TextBox categoryTextBox = (e.Item.FindControl("CategoryTextBox") as TextBox); this.ValidateCategoryTextBox(titleTextBox); string newCategory = Server.HtmlEncode(categoryTextBox.Text); Category enteredCategory = db.Categories.FirstOrDefault(cat => cat.Name.ToLower() == newCategory.ToLower()); if (enteredCategory == null) { enteredCategory = new Category() { Name = newCategory, }; db.Categories.Add(enteredCategory); db.SaveChanges(); } string categoryId = enteredCategory.Id.ToString(); string currDate = DateTime.Now.ToString(); this.EntityDataSourceTodos.InsertParameters.Add("CategoryId", TypeCode.Int32, categoryId); this.EntityDataSourceTodos.InsertParameters.Add("LastUpdated", TypeCode.DateTime, currDate); this.LabelSuccessMessages.Text = "Todo successfuly added!"; this.ListViewTodos.DataBind(); } catch (Exception ex) { this.LabelErrorMessages.Text = ex.Message; e.Cancel = true; } finally { if (db != null) { db.Dispose(); } } }
protected void ButtonAddCategory_Click(object sender, EventArgs e) { using (var context = new ToDosDbEntities()) { string categoryName = (this.GridViewCategories.FooterRow.FindControl("TextBoxNewName") as TextBox).Text; var category = context.Categories.FirstOrDefault(c => string.Compare(c.Name, categoryName, true) == 0); if (category == null) { category = new Category { Name = categoryName }; context.Categories.Add(category); context.SaveChanges(); Response.Redirect(Request.RawUrl); } } }
protected void ListViewTodos_ItemUpdating(object sender, ListViewUpdateEventArgs e) { ToDosDbEntities db = new ToDosDbEntities(); this.LabelErrorMessages.Text = string.Empty; this.LabelSuccessMessages.Text = string.Empty; try { ListViewItem item = this.ListViewTodos.Items[e.ItemIndex]; TextBox titleTextBox = (item.FindControl("TitleEditTextBox") as TextBox); this.ValidateTitleTextBox(titleTextBox); TextBox bodyTextBox = (item.FindControl("BodyEditTextBox") as TextBox); this.ValidateBodyTextBox(bodyTextBox); TextBox categoryTextBox = (item.FindControl("CategoryEditTextBox") as TextBox); this.ValidateCategoryTextBox(categoryTextBox); string newCategory = Server.HtmlEncode(categoryTextBox.Text); int index = this.ListViewTodos.EditIndex; DataKey dataKey = this.ListViewTodos.DataKeys[index]; string todoId = dataKey.Value.ToString(); var currTodo = db.ToDos.FirstOrDefault(todo => todo.Id.ToString() == todoId); if (currTodo == null) { throw new ArgumentException("Current Todo was not found!"); } Category enteredCategory = db.Categories.FirstOrDefault(cat => cat.Name.ToLower() == newCategory.ToLower()); if (enteredCategory == null) { enteredCategory = new Category() { Name = newCategory, }; db.Categories.Add(enteredCategory); db.SaveChanges(); } if (currTodo.CategoryId != enteredCategory.Id) { currTodo.CategoryId = enteredCategory.Id; this.LabelErrorMessages.Text = "Successfuly edited!"; } currTodo.LastUpdated = DateTime.Now; db.SaveChanges(); } catch (Exception ex) { this.LabelErrorMessages.Text = ex.Message; e.Cancel = true; } finally { if (db != null) { db.Dispose(); } } }