protected void btnSave_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtTitle.Text) && !string.IsNullOrEmpty(txtBody.Text) && ddlCategories.SelectedIndex >= 0) { int categoryId = int.Parse(ddlCategories.SelectedValue); int todoId = 0; if (!string.IsNullOrEmpty(btnSave.CommandArgument)) { todoId = int.Parse(btnSave.CommandArgument); } var db = new TodosEntities(); var todo = db.Todoes.Find(todoId); if (todo == null) { todo = new Todo(); } todo.Title = txtTitle.Text; todo.Body = txtBody.Text; todo.CategoryId = categoryId; todo.LastChangeDate = DateTime.Now; db.Todoes.AddOrUpdate(todo); db.SaveChanges(); txtTitle.Text = string.Empty; txtBody.Text = string.Empty; btnSave.CommandArgument = string.Empty; grdTodos.DataBind(); } }
protected void btnSave_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtName.Text)) { int categoryId = 0; if (!string.IsNullOrEmpty(btnSave.CommandArgument)) { categoryId = int.Parse(btnSave.CommandArgument); } var db = new TodosEntities(); var c = db.Categories.Find(categoryId); if (c == null) { c = new Category(); } c.Name = txtName.Text; db.Categories.AddOrUpdate(c); db.SaveChanges(); txtName.Text = string.Empty; btnSave.CommandArgument = string.Empty; lbCategories.DataBind(); } }