protected void LinkButtonCreateCategory_Click(object sender, EventArgs e) { LibrarySystemEntities context = new LibrarySystemEntities(); Category newCategory = new Category() { Name = this.TextBoxCategoryName.Text }; if (newCategory.Name.Length > MaxCategoryNameLength) { ErrorSuccessNotifier.AddErrorMessage("The category name is too long, it has to be up to " + MaxCategoryNameLength + " characters."); return; } if (newCategory.Name == string.Empty) { ErrorSuccessNotifier.AddErrorMessage("The category name is mandatory."); return; } context.Categories.Add(newCategory); context.SaveChanges(); this.PanelCreateCategory.Visible = false; this.TextBoxCategoryName.Text = string.Empty; this.LinkButtonShowCreatePanel.Visible = true; }
public Book EditBook(Book _book) { try { using (var context = new LibrarySystemEntities()) { Book oldbook = context.Books.FirstOrDefault(c => c.BookId == _book.BookId); if (oldbook != null) { oldbook.BookName = _book.BookName; oldbook.BookPages = _book.BookPages; context.SaveChanges(); return oldbook; } return null; } } catch (Exception) { throw; } }
protected void EditBookButton_Command(object sender, CommandEventArgs e) { try { var context = new LibrarySystemEntities(); int bookId = Convert.ToInt32(e.CommandArgument); var book = context.Books.Find(bookId); book.Title = this.TexboxForBookTitle.Text; book.Author = this.TexboxForBookAuthor.Text; book.ISBN = this.TexboxForBookISBN.Text; book.Description = this.TexboxForBookDescription.Text; book.WebSite = this.TexboxForBookWebSiet.Text; int selectedCategoryId = Convert.ToInt32(this.SelectForBookCategory.SelectedValue); Category selectedCategory = context.Categories.FirstOrDefault(c => c.Id == selectedCategoryId); book.Category = selectedCategory; context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Book '" + book.Title + "' edited."); this.editBookConteiner.Visible = false; this.AllBooks.SelectMethod = "AllBooks_GetData"; this.AllBooks.DataBind(); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
protected void OnButtonDeleteBook_Command(object sender, CommandEventArgs e) { using (var context = new LibrarySystemEntities()) { int bookId = Convert.ToInt32(e.CommandArgument); var book = context.Books.Find(bookId); if (book != null) { try { context.Books.Remove(book); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Book deleted"); this.PanelDeleteBook.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage("This book does not exist anymore"); } } else { ErrorSuccessNotifier.AddErrorMessage("This book does not exist anymore"); } } }
protected void OnButtonDeleteCategory_Command(object sender, CommandEventArgs e) { using (var context = new LibrarySystemEntities()) { int categoryId = Convert.ToInt32(e.CommandArgument); var category = context.Categories.Find(categoryId); if (category != null) { try { var books = category.Books.ToList(); foreach (var book in books) { context.Books.Remove(book); } context.Categories.Remove(category); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category deleted"); this.GridViewCategories.SetPageIndex(this.GridViewCategories.PageIndex); this.PanelDeleteCategory.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } else { ErrorSuccessNotifier.AddErrorMessage("This category does not exist anymore"); } } }
protected void DeleteAction_Command(object sender, CommandEventArgs e) { if (e.CommandArgument == "Yes") { if (this.BookId < 0) { Error_Handler_Control.ErrorSuccessNotifier .AddErrorMessage("Books' id was lost. Please try again;"); Response.Redirect("~/Registered/EditBooks.aspx"); } try { using (var dbContex = new LibrarySystemEntities()) { var book = dbContex.Books.Find(this.BookId); dbContex.Books.Remove(book); dbContex.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Deleted!"); this.BooksGridView.DataBind(); this.DeleteConfirmationPanel.Visible = false; } } catch (EntityDataSourceValidationException ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } } }
protected void AddCategoryButton_Click(object sender, EventArgs e) { var newCategoryName = this.CategoryAddTextBox.Text; if (this.ValidateCategoryName(newCategoryName)) { using (var dbContext = new LibrarySystemEntities()) { try { dbContext.Categories.Add(new Category { Name = newCategoryName }); dbContext.SaveChanges(); Error_Handler_Control.ErrorSuccessNotifier .AddSuccessMessage("Added new!"); this.CategoriesGridView.DataBind(); } catch (EntityDataSourceValidationException ex) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage(ex); } catch (Exception ex) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage(ex.Message); } } } }
protected void UpdateBookBotton_Click(object sender, EventArgs e) { if (!this.ValidataEditBookEntries()) { return; } if (this.BookId < 0) { Error_Handler_Control.ErrorSuccessNotifier .AddErrorMessage("Book's id was unclear. Please go back and try again."); return; } using (var dbContext = new LibrarySystemEntities()) { try { var category = this.EditBookCategoryDropDown.SelectedValue; int categoryId; if (!int.TryParse(category, out categoryId)) { throw new ArgumentException("Book's category was unclear. Please go back and try again."); } var book = dbContext.Books.Find(BookId); book.Title = this.EditBookTitle.Text; book.Author = this.EditBookAuthor.Text; book.ISBN = this.EditBookISBN.Text; book.Url = this.EditBookUrl.Text; book.Description = this.EditBookDescription.InnerText; if (book.CategoryId != categoryId) { book.Category = dbContext.Categories.Find(categoryId); } dbContext.SaveChanges(); this.ClearAndHideEditForm(); Error_Handler_Control.ErrorSuccessNotifier.AddSuccessMessage("Edited!"); } catch (EntityDataSourceValidationException ex) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage(ex); } catch (Exception ex) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage(ex.Message); } } }
public Book AddBook(Book _book) { try { using (var context = new LibrarySystemEntities()) { context.Books.Add(_book); context.SaveChanges(); return _book; } } catch (Exception) { throw; } }
protected void OnButtonCreateCategory_Command(object sender, CommandEventArgs e) { using (var context = new LibrarySystemEntities()) { try { string categoryName = this.TextBoxCreateCategoryName.Text.Trim(); if (categoryName == "") { ErrorSuccessNotifier.AddErrorMessage("The category cannot be empty"); return; } var existingCategory = context.Categories.FirstOrDefault( cat => cat.Name.ToLower() == categoryName.ToLower()); if (existingCategory != null) { this.PanelCreateCategory.Visible = false; ErrorSuccessNotifier.AddErrorMessage("This category already exists"); return; } var newCategory = new Category() { Name = categoryName }; context.Categories.Add(newCategory); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category created"); this.GridViewCategories.SetPageIndex(this.GridViewCategories.PageIndex); this.PanelCreateCategory.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
protected void ButtonSave_Click(object sender, EventArgs e) { var id = this.CategoryId; if (id < 0) { Error_Handler_Control.ErrorSuccessNotifier .AddErrorMessage("Category was NOT saved. Please try again!"); return; } using (var dbContext = new LibrarySystemEntities()) { var category = dbContext.Categories.Find(id); if (category == null) { Error_Handler_Control.ErrorSuccessNotifier .AddErrorMessage("Category was not found on server. Please try again!"); return; } if (this.ValidateCategoryName(this.CategoryEditTextBox.Text)) { category.Name = this.CategoryEditTextBox.Text; try { dbContext.SaveChanges(); Error_Handler_Control.ErrorSuccessNotifier.AddSuccessMessage("Saved!"); this.CloseEdit(); } catch (EntityDataSourceValidationException ex) { Error_Handler_Control.ErrorSuccessNotifier .AddErrorMessage(ex); } } } }
protected void CreateNewBookButton_Click(object sender, EventArgs e) { int newBookCategoryId; var validCategoryId = int.TryParse(this.NewBookCategoryDropDown.SelectedValue, out newBookCategoryId); if (!ValidateInsertFields() || !validCategoryId) { return; } using (var dbContext = new LibrarySystemEntities()) { try { dbContext.Books.Add(new Book { Title = this.NewBookTitle.Text, Author = this.NewBookAuthor.Text, ISBN = this.NewBookISBN.Text, Url = this.NewBookUrl.Text, Description = this.NewBookDescription.InnerText, Category = dbContext.Categories.Find(newBookCategoryId) }); dbContext.SaveChanges(); this.CleanAndHideInsertForm(); ErrorSuccessNotifier.AddSuccessMessage("Created!"); } catch (EntityDataSourceValidationException ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } } }
protected void EditCategoryButton_Command(object sender, CommandEventArgs e) { try { var context = new LibrarySystemEntities(); int categoryId = Convert.ToInt32(e.CommandArgument); var category = context.Categories.Find(categoryId); category.Title = this.TexboxForCategoryToEdit.Text; context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category '" + category.Title + "' edited successfuly."); this.editCategoryConteiner.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } this.AllCategories.SelectMethod = "AllCategories_GetData"; this.AllCategories.DataBind(); }
protected void CreateCategoryButton_Command(object sender, CommandEventArgs e) { try { var context = new LibrarySystemEntities(); Category category = new Category(); category.Title = this.TexboxForCategoryToCreate.Text; context.Categories.Add(category); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category '" + category.Title + "' created."); this.createCategoryConteiner.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } this.AllCategories.SelectMethod = "AllCategories_GetData"; this.AllCategories.DataBind(); }
protected void OnButtonSaveCategory_Command(object sender, CommandEventArgs e) { using (var context = new LibrarySystemEntities()) { int categoryId = Convert.ToInt32(e.CommandArgument); var category = context.Categories.Find(categoryId); if (category != null) { try { string categoryName = this.TextBoxEditCategoryName.Text; if (categoryName == "") { ErrorSuccessNotifier.AddErrorMessage("The category cannot be empty"); return; } category.Name = categoryName; context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category modified"); this.GridViewCategories.SetPageIndex(this.GridViewCategories.PageIndex); this.PanelEditCategory.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } else { ErrorSuccessNotifier.AddErrorMessage("This category does not exist anymore"); } } }
protected void DeleteBookButton_Command(object sender, CommandEventArgs e) { try { var context = new LibrarySystemEntities(); int bookId = Convert.ToInt32(e.CommandArgument); var book = context.Books.Find(bookId); context.Books.Remove(book); context.SaveChanges(); this.AllBooks.SelectMethod = "AllBooks_GetData"; this.AllBooks.DataBind(); ErrorSuccessNotifier.AddInfoMessage("Book '" + book.Title + "' deleted."); this.deleteBookConteiner.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
protected void HandleConfirmationCommand(object sender, CommandEventArgs e) { if (e.CommandArgument == "Yes") { using (var dbContext = new LibrarySystemEntities()) { var category = dbContext.Categories.Find(this.CategoryId); if (category == null) { Error_Handler_Control.ErrorSuccessNotifier .AddErrorMessage("Category could not be deleted. Try again later!"); return; } try { dbContext.Books.RemoveRange(category.Books); dbContext.Categories.Remove(category); dbContext.SaveChanges(); Error_Handler_Control.ErrorSuccessNotifier.AddSuccessMessage("Deleted"); this.CategoriesGridView.DataBind(); } catch (EntityDataSourceValidationException ex) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage(ex); } catch (Exception ex) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage(ex.Message); } } } this.CategoryId = -1; this.DeleteConfirmationPanel.Visible = false; }
protected void DeleteCategoryButton_Command(object sender, CommandEventArgs e) { try { var context = new LibrarySystemEntities(); int categoryId = Convert.ToInt32(e.CommandArgument); var category = context.Categories.Find(categoryId); context.Books.RemoveRange(category.Books); context.Categories.Remove(category); context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Category '" + category.Title + "' deleted along with all related books."); this.deleteCategoryConteiner.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } this.AllCategories.SelectMethod = "AllCategories_GetData"; this.AllCategories.DataBind(); }
protected void LinkButtonEditCategory_Click(object sender, EventArgs e) { LibrarySystemEntities context = new LibrarySystemEntities(); int categoryId = Convert.ToInt32(this.HiddenField.Text); Category category = context.Categories.Find(categoryId); category.Name = this.TextBoxEditedCategoryName.Text; if (category.Name == string.Empty) { ErrorSuccessNotifier.AddErrorMessage("The category name is mandatory."); return; } if (category.Name.Length >= MaxCategoryNameLength) { ErrorSuccessNotifier.AddErrorMessage("The category name is too long, it has to be up to " + MaxCategoryNameLength + " characters."); return; } context.SaveChanges(); this.PanelEditCategory.Visible = false; this.LinkButtonShowCreatePanel.Visible = true; }
public Reader EditReader(Reader _reader) { try { using (var context = new LibrarySystemEntities()) { Reader oldreader = context.Readers.FirstOrDefault(c => c.ReaderId == _reader.ReaderId); if (oldreader != null) { oldreader.FirstName = _reader.FirstName; oldreader.LastName = _reader.LastName; context.SaveChanges(); return oldreader; } return null; } } catch (Exception) { throw; } }
public bool DeleteReader(int _id) { try { using (var context = new LibrarySystemEntities()) { var reader = context.Readers.FirstOrDefault(cstm => cstm.ReaderId == _id); context.Readers.Remove(reader); int i = context.SaveChanges(); return i > 0; } } catch (Exception) { throw; } }
protected void OnButtonCreateBook_Command(object sender, CommandEventArgs e) { using (var context = new LibrarySystemEntities()) { try { bool error = false; int categoryId = Convert.ToInt32(this.DropDownCreateCategories.SelectedValue); var category = context.Categories.Find(categoryId); string title = this.TextBoxCreateBookTitle.Text.Trim(); if (title == null || title == "") { ErrorSuccessNotifier.AddErrorMessage("The title field is required"); error = true; } else { var existingBook = context.Books.FirstOrDefault(b => b.Title.ToLower() == title.ToLower()); if (existingBook != null) { ErrorSuccessNotifier.AddErrorMessage("A book with the same title already exist"); return; } } string authors = this.TextBoxCreateBookAuthors.Text; if (authors == null || authors == "") { ErrorSuccessNotifier.AddErrorMessage("The author(s) field is required"); error = true; } if (error) { return; } string isbn = this.TextBoxCreateBookISBN.Text; if (isbn != "") { var existingBook = context.Books.FirstOrDefault(b => b.ISBN.ToLower() == isbn.ToLower()); if (existingBook != null) { ErrorSuccessNotifier.AddErrorMessage("A book with the same ISBN already exist"); return; } } string webSite = this.TextBoxCreateBookWebSite.Text; string description = this.TextBoxCreateBookDescription.Text; var newBook = new Book(); newBook.Title = title; newBook.Author = authors; newBook.Category = category; if (isbn != "") { newBook.ISBN = isbn; } if (webSite != "") { newBook.WebSite = webSite; } if (description != "") { newBook.Description = description; } context.Books.Add(newBook); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Book created"); this.PanelCreateBook.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
public BorrowInfo AddBorrowInfo(BorrowInfo _borrow) { try { using (var context = new LibrarySystemEntities()) { context.BorrowInfoes.Add(_borrow); context.SaveChanges(); return _borrow; } } catch (Exception) { throw; } }
protected void LinkButtonDeleteCategory_Command(object sender, CommandEventArgs e) { if (e.CommandName == CommandDeleteCategory) { int categoryId = Convert.ToInt32(e.CommandArgument.ToString()); LibrarySystemEntities context = new LibrarySystemEntities(); Category categoryToRemove = context.Categories.Find(categoryId); context.Books.RemoveRange(categoryToRemove.Books); context.Categories.Remove(categoryToRemove); context.SaveChanges(); } }
public BorrowInfo EditBorrowInfo(BorrowInfo _borrow) { try { using (var context = new LibrarySystemEntities()) { BorrowInfo oldborrow = context.BorrowInfoes.FirstOrDefault(c => c.BorrowId == _borrow.BorrowId); if (oldborrow != null) { oldborrow.BorrowDate = _borrow.BorrowDate; oldborrow.ReturnDate = _borrow.ReturnDate; context.SaveChanges(); return oldborrow; } return null; } } catch (Exception) { throw; } }
public Reader AddReader(Reader _reader) { try { using (var context = new LibrarySystemEntities()) { context.Readers.Add(_reader); context.SaveChanges(); return _reader; } } catch (Exception) { throw; } }
protected void LinkButtonEditBook_Click(object sender, EventArgs e) { LibrarySystemEntities context = new LibrarySystemEntities(); var selectedCategoryName = this.DropDownListEditedCategories.SelectedValue; var selectedCategoryId = context.Categories.Where(c => c.Name == selectedCategoryName).FirstOrDefault().CategoryId; int bookId = Convert.ToInt32(this.HiddenField.Text); Book editedBook = context.Books.Find(bookId); editedBook.Title = this.TextBoxEditedTitle.Text; editedBook.Author = this.TextBoxEditedAuthor.Text; editedBook.ISBN = this.TextBoxEditedISBN.Text; editedBook.WebSite = this.TextBoxEditedWebSite.Text; editedBook.Description = this.TextBoxEditedDescription.Text; editedBook.CategoryId = selectedCategoryId; if (editedBook.Title.Length >= MaxTitleLength) { ErrorSuccessNotifier.AddErrorMessage("The book title is too long, it has to be up to " + MaxTitleLength + " characters."); return; } if (editedBook.Author.Length >= MaxAuthorLength) { ErrorSuccessNotifier.AddErrorMessage("The book author(s) is too long, it has to be up to " + MaxAuthorLength + " characters."); return; } if (editedBook.ISBN.Length >= MaxISBNLength) { ErrorSuccessNotifier.AddErrorMessage("The book website is too long, it has to be up to " + MaxISBNLength + " characters."); return; } if (editedBook.WebSite.Length >= MaxWebSiteLength) { ErrorSuccessNotifier.AddErrorMessage("The book website is too long, it has to be up to " + MaxWebSiteLength + " characters."); return; } context.SaveChanges(); this.PanelEditBook.Visible = false; this.PanelCreateBook.Visible = false; this.LinkButtonShowCreatePanel.Visible = true; }
public bool DeleteBorrow(int _id) { try { using (var context = new LibrarySystemEntities()) { var borrow = context.BorrowInfoes.FirstOrDefault(cstm => cstm.BorrowId == _id); context.BorrowInfoes.Remove(borrow); int i = context.SaveChanges(); return i > 0; } } catch (Exception) { throw; } }
protected void LinkButtonDeleteBook_Command(object sender, CommandEventArgs e) { if (e.CommandName == CommandDeleteBook) { int bookId = Convert.ToInt32(e.CommandArgument.ToString()); LibrarySystemEntities context = new LibrarySystemEntities(); Book bookToRemove = context.Books.Find(bookId); context.Books.Remove(bookToRemove); context.SaveChanges(); } }
protected void OnButtonEditBook_Command(object sender, CommandEventArgs e) { using (var context = new LibrarySystemEntities()) { int bookId = Convert.ToInt32(e.CommandArgument); var book = context.Books.Find(bookId); if (book != null) { try { bool error = false; int categoryId = Convert.ToInt32(this.DropDownEditCategories.SelectedValue); var category = context.Categories.Find(categoryId); string title = this.TextBoxEditBookTitle.Text.Trim(); if (title == null || title == "") { ErrorSuccessNotifier.AddErrorMessage("The title field is required"); error = true; } string authors = this.TextBoxEditBookAuthors.Text; if (authors == null || authors == "") { ErrorSuccessNotifier.AddErrorMessage("The author(s) field is required"); error = true; } if (error) { return; } string isbn = this.TextBoxEditBookISBN.Text; string webSite = this.TextBoxEditBookWebSite.Text; string description = this.TextBoxEditBookDescription.Text; book.Title = title; book.Author = authors; book.Category = category; if (isbn != "") { book.ISBN = isbn; } if (webSite != "") { book.WebSite = webSite; } if (description != "") { book.Description = description; } context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Book modified"); this.PanelEditBook.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } else { ErrorSuccessNotifier.AddErrorMessage("This book does not exist anymore"); } } }
protected void LinkButtonCreateBook_Click(object sender, EventArgs e) { LibrarySystemEntities context = new LibrarySystemEntities(); var selectedCategoryName = this.DropDownListCategories.SelectedValue; var selectedCategoryId = context.Categories.Where(c => c.Name == selectedCategoryName).FirstOrDefault().CategoryId; Book newBook = new Book() { Title = this.TextBoxTitle.Text, Author = this.TextBoxAuthor.Text, ISBN = this.TextBoxISBN.Text, WebSite = this.TextBoxWebSite.Text, Description = this.TextBoxDescription.Text, CategoryId = selectedCategoryId }; if (newBook.Title.Length >= MaxTitleLength) { ErrorSuccessNotifier.AddErrorMessage("The book title is too long, it has to be up to " + MaxTitleLength + " characters."); return; } if (newBook.Author.Length >= MaxAuthorLength) { ErrorSuccessNotifier.AddErrorMessage("The book author(s) is too long, it has to be up to " + MaxAuthorLength + " characters."); return; } if (newBook.ISBN.Length >= MaxISBNLength) { ErrorSuccessNotifier.AddErrorMessage("The book website is too long, it has to be up to " + MaxISBNLength + " characters."); return; } if (newBook.WebSite.Length >= MaxWebSiteLength) { ErrorSuccessNotifier.AddErrorMessage("The book website is too long, it has to be up to " + MaxWebSiteLength + " characters."); return; } if (newBook.Title == string.Empty) { ErrorSuccessNotifier.AddErrorMessage("The book title is mandatory."); return; } if (newBook.Author == string.Empty) { ErrorSuccessNotifier.AddErrorMessage("The book author is mandatory."); return; } context.Books.Add(newBook); context.SaveChanges(); this.PanelCreateBook.Visible = false; this.LinkButtonShowCreatePanel.Visible = true; }
public User AddUser(User _user) { try { using (var context = new LibrarySystemEntities()) { context.Users.Add(_user); context.SaveChanges(); return _user; } } catch (Exception) { throw; } }