protected void LinkButtonCreateOrEdit_Click(object sender, EventArgs e) { Book book; int bookId= 0; BookLibraryEntities db = new BookLibraryEntities(); if (!String.IsNullOrEmpty(this.LabelBookId.Text)) { bookId = Convert.ToInt32(this.LabelBookId.Text); book = db.Books.FirstOrDefault(c => c.Id == bookId); } else { book = new Book(); db.Books.Add(book); } try { if(String.IsNullOrEmpty(this.TextBoxTitle.Text)) { throw new ArgumentException("Title must be filled!"); } if (String.IsNullOrEmpty(this.TextBoxAuthors.Text)) { throw new ArgumentException("Author must be filled!"); } book.Title = this.TextBoxTitle.Text; book.Author = this.TextBoxAuthors.Text; book.ISBN = this.TextBoxISBN.Text; book.Website = this.TextBoxWebsite.Text; book.Description = this.TextBoxDescription.Text; book.CategoryId = Convert.ToInt32(this.DropDownListCategories.SelectedItem.Value); db.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Book successfully " + (bookId == 0 ? "created!" : "edited!")); ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("EditBooks.aspx", false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
protected void saveNewBookButton_Click(object sender, EventArgs e) { try { var title = this.newTitleBox.Text; var author = this.newAuthorBox.Text; if (string.IsNullOrWhiteSpace(title) || string.IsNullOrWhiteSpace(author)) { throw new ArgumentNullException( "The title or the author of a book could not be null or white space"); } var isbn = this.newISBNBox.Text; var website = this.newWebSiteBox.Text; var pattern = @"^(https?|ftp|file)://.+$"; if (!Regex.IsMatch(website, pattern)) { throw new ArgumentOutOfRangeException( "The book web site url is not valid"); } var description = this.newDescriptionBox.Text; var categoryId = Convert.ToInt32(Session["categoryId"]); Session["categoryId"] = null; LibrarySystemEntities context = new LibrarySystemEntities(); Book book = new Book() { Title = title, Author = author, ISBN = isbn, WebSite = website, Discription = description }; Category category = context.Categories.Find(categoryId); category.Books.Add(book); context.SaveChanges(); this.booksGrid.PageIndex = 0; this.newBookPanel.Visible = false; this.newBookButton.Visible = true; ErrorSuccessNotifier.AddSuccessMessage("Book was successfully added."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }