protected void LinkButtonCreateOrEdit_Click(object sender, EventArgs e) { Category category; int categoryId = 0; BookLibraryEntities db = new BookLibraryEntities(); if(!String.IsNullOrEmpty(this.LabelCategoryId.Text)) { categoryId = Convert.ToInt32(this.LabelCategoryId.Text); category = db.Categories.FirstOrDefault(c => c.Id == categoryId); } else { category = new Category(); db.Categories.Add(category); } string categoryName = this.TextBoxCreateOrEdit.Text; try { if(String.IsNullOrEmpty(categoryName)) { throw new ArgumentException("Category name is required!"); } category.Name = categoryName; db.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Category " + (categoryId == 0 ? "created!" : "edited!")); ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("EditCategories.aspx", false); } catch(Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
protected void saveNewCategoryButton_Click(object sender, EventArgs e) { try { var name = this.newCategoryBox.Text; if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException( "The name of a category could not be null or white space"); } LibrarySystemEntities context = new LibrarySystemEntities(); Category category = new Category { Name = name }; context.Categories.Add(category); context.SaveChanges(); this.categoriesGrid.PageIndex = 0; this.newCategoryPanel.Visible = false; this.newCategoryButton.Visible = true; ErrorSuccessNotifier.AddSuccessMessage("Category was successfully added."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }