protected void Page_Load(object sender, EventArgs e) { var context = new LibrarySystemEntities(); try { var bookId = Convert.ToInt32(this.Request.Params["bookId"]); var book = context.Books.FirstOrDefault(b => b.BookId == bookId); if (book == null) { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occurred! The book you want to view was not found..."); } else { this.LiteralBookTitle.Text = this.Server.HtmlEncode(book.Title); this.LiteralBookAuthors.Text = this.Server.HtmlEncode(string.Format("by {0}", book.Authors)); this.LiteralBookIsbn.Text = this.Server.HtmlEncode(string.Format("ISBN {0}", book.ISBN)); this.HyperLinkWebSite.Text = this.Server.HtmlEncode(book.WebSite); this.HyperLinkWebSite.NavigateUrl = this.Server.HtmlEncode(book.WebSite); this.LiteralBookDescription.Text = this.Server.HtmlEncode(book.Description); this.HyperLinkBackToBooks.NavigateUrl = "Default.aspx"; } } catch (Exception exc) { ErrorSuccessNotifier.AddErrorMessage(exc); } }
protected void Page_Load(object sender, EventArgs e) { var searchQuery = Request.Params["q"]; var context = new LibrarySystemEntities(); if (!string.IsNullOrEmpty(searchQuery)) { var results = context.Books .Where(r => r.Title.Contains(searchQuery) || r.Author.Contains(searchQuery)) .OrderBy(b => b.Author) .ThenBy(x => x.Author) .ToList(); this.searchResultsTitle.Text = searchQuery; this.searchResults.DataSource = results; this.searchResults.DataBind(); } else { var results = context.Books .OrderBy(b => b.Author) .ThenBy(x => x.Author) .ToList(); this.searchResultsTitle.Text = searchQuery; this.searchResults.DataSource = results; this.searchResults.DataBind(); } }
protected void Page_Load(object sender, EventArgs e) { int bookId; bool tryParseId = int.TryParse(Request.Params["id"], out bookId); if (Request.Params["Id"] == null || !tryParseId) { ErrorSuccessNotifier.AddErrorMessage("Invalid id!"); this.Response.Redirect("~/"); } var context = new LibrarySystemEntities(); var book = context.Books.FirstOrDefault(b => b.Id == bookId); if (book == null) { ErrorSuccessNotifier.AddErrorMessage("Invalid book!"); this.Response.Redirect("~/"); } this.BookTitle.InnerText = book.Title; this.BookAuthor.InnerText = "by " + book.Author; this.BookISBN.InnerText = "ISBN " + book.ISBN; this.BookWebSiteURL.HRef = book.WebSite; this.BookWebSiteURL.InnerText = book.WebSite; this.BookDescription.InnerText = book.Description; }
protected void Page_PreRender(object sender, EventArgs e) { try { var id = int.Parse(Request.Params["bookId"]); using (var context = new LibrarySystemEntities()) { var book = context.Books.Find(id); if (book == null) { ErrorSuccessNotifier.AddErrorMessage("Book not found!"); return; } this.LabelTitle.InnerText = book.Title; this.LabelAuthor.Text = "by " + book.Author; this.BookISBN.InnerText = book.ISBN != null?"ISBN: " + book.ISBN : "No ISBN for this book"; this.BookUrl.Text = book.Url != null ? "Web site: <a href=" + Server.HtmlEncode(book.Url) + ">" + Server.HtmlEncode(book.Url) + "</a>" : "No website for this book"; this.BookDescription.InnerText = book.Description; } } catch (EntityDataSourceValidationException ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } }
protected void Page_PreRender(object sender, EventArgs e) { try { var currBookId = Convert.ToInt32(Request.QueryString["id"]); LibrarySystemEntities db = new LibrarySystemEntities(); using (db) { this.bookDetails.DataSource = db.Books.Where(b => b.id == currBookId).ToList(); this.bookDetails.DataBind(); } } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. ErrorSuccessNotifier.AddErrorMessage(exceptionMessage + " " + ex.EntityValidationErrors); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } }
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 OnButtonShowEditBookPanel_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) { this.ShowEditBookPanel(); this.ButtonEditBook.CommandArgument = bookId.ToString(); this.DropDownEditCategories.SelectedValue = book.CategoryId.ToString(); this.TextBoxEditBookAuthors.Text = book.Author; this.TextBoxEditBookDescription.Text = book.Description; this.TextBoxEditBookISBN.Text = book.ISBN; this.TextBoxEditBookTitle.Text = book.Title; this.TextBoxEditBookWebSite.Text = book.WebSite; } else { ErrorSuccessNotifier.AddErrorMessage("This book 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 Page_PreRender(object sender, EventArgs e) { var context = new LibrarySystemEntities(); var categories = context.Categories.Include(c => c.Books).OrderBy(c => c.CategoryId); this.ListViewCategories.DataSource = categories.ToList(); this.ListViewCategories.DataBind(); }
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 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 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"); } } }
// The return type can be changed to IEnumerable, however to support // paging and sorting, the following parameters must be added: // int maximumRows // int startRowIndex // out int totalRowCount // string sortByExpression public IQueryable <Book> BooksInCategory_GetData() { var query = Request.Params["query"]; if (query == null) { Response.Redirect("~/Default.aspx"); } if (query.Length > 200) { Error_Handler_Control.ErrorSuccessNotifier .AddErrorMessage("Query was too long. Allowed up to 200 symbols"); return(new List <Book>().AsQueryable()); } var dbContext = new LibrarySystemEntities(); IQueryable <Book> books = dbContext.Books; if (!string.IsNullOrEmpty(query)) { var queryToLower = query.ToLower(); books = books.Where(b => b.Author.IndexOf(queryToLower) >= 0 || b.Title.IndexOf(queryToLower) >= 0); } return(books.OrderBy(b => b.Title).ThenBy(b => b.Author)); }
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; }
protected void Page_Load(object sender, EventArgs e) { var context = new LibrarySystemEntities(); var employeesDataSource = context.Categories.ToList(); this.RepeaterCategories.DataSource = employeesDataSource; this.RepeaterCategories.DataBind(); }
public IQueryable <Category> BooksRepeater_GetData() { var dbContex = new LibrarySystemEntities(); return(dbContex.Categories .Include("Books") .OrderBy(c => c.Id)); }
// The return type can be changed to IEnumerable, however to support // paging and sorting, the following parameters must be added: // int maximumRows // int startRowIndex // out int totalRowCount // string sortByExpression public IQueryable<Category> CategoriesListView_GetData() { var context = new LibrarySystemEntities(); var categories = context.Categories.Include("Books"); return categories; }
public List <User> GetUserData_DAL() { using (LibrarySystemEntities db = new LibrarySystemEntities()) { var query = (from u in db.Users select u).ToList(); return(query); } }
public List <Book> GetBookData_DAL() { using (LibrarySystemEntities db = new LibrarySystemEntities()) { // To list the Book using LINQ Queries var query = (from b in db.Books select b).ToList(); return(query); } }
protected void OpenInsertForm_Click(object sender, EventArgs e) { var dbContex = new LibrarySystemEntities(); this.NewBookCategoryDropDown.DataSource = dbContex.Categories.ToList(); this.NewBookCategoryDropDown.DataBind(); this.AddBookPanel.Visible = true; }
protected void Page_Load(object sender, EventArgs e) { LibrarySystemEntities context = new LibrarySystemEntities(); var categories = context.Categories.ToList(); this.RepeaterCategories.DataSource = categories; this.RepeaterCategories.DataBind(); this.IncludeCss(); }
protected void SeeBookDetails(object sender, CommandEventArgs e) { int answerId = Convert.ToInt32(e.CommandArgument); using (LibrarySystemEntities context = new LibrarySystemEntities()) { Book book = context.Books.FirstOrDefault(x => x.Id == answerId); Response.Redirect("SeeBookDetails.aspx?bookId=" + book.Id); } }
protected void Page_PreRender(object sender, EventArgs e) { using (var context = new LibrarySystemEntities()) { var categories = context.Categories.ToList(); this.DropDownCreateCategories.DataSource = categories; this.DropDownEditCategories.DataSource = categories; this.DataBind(); } }
protected void CreateABookButton_Command(object sender, CommandEventArgs e) { var context = new LibrarySystemEntities(); this.createCategoryConteiner.Visible = true; this.deleteBookConteiner.Visible = false; this.editBookConteiner.Visible = false; this.CreateSelectForBookCategory.DataSource = context.Categories.ToList(); this.CreateSelectForBookCategory.DataBind(); }
public LanguagesController() { mapperConfiguration = new MapperConfiguration(cfg => { cfg.CreateMap <Language, LanguageDto>(); }); mapper = mapperConfiguration.CreateMapper(); db = new LibrarySystemEntities(); db.Configuration.ProxyCreationEnabled = false; }
// The id parameter name should match the DataKeyNames value set on the control public void AllCategories_DeleteItem(int id) { this.editCategoryConteiner.Visible = false; this.createCategoryConteiner.Visible = false; this.deleteCategoryConteiner.Visible = true; var context = new LibrarySystemEntities(); var category = context.Categories.Find(id); this.NameOfDeleteCategory.Text = "Delete " + category.Title + " category"; this.DeleteCategoryButton.CommandArgument = category.Id.ToString(); }
// The id parameter name should match the DataKeyNames value set on the control public void AllBooks_DeleteItem(int id) { this.deleteBookConteiner.Visible = true; this.editBookConteiner.Visible = false; this.createCategoryConteiner.Visible = false; var context = new LibrarySystemEntities(); var book = context.Books.Find(id); this.NameOfDeleteBook.Text = "Delete " + book.Title + " book"; this.DeleteBookButton.CommandArgument = book.Id.ToString(); }
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 AuthorsController() { mapperConfiguration = new MapperConfiguration(cfg => { cfg.CreateMap <Author, AuthorDto>(); cfg.CreateMap <Nationality, NationalityDto>(); }); mapper = mapperConfiguration.CreateMapper(); db = new LibrarySystemEntities(); db.Configuration.ProxyCreationEnabled = false; }
public List<Book> GetBooks() { try { using (var context = new LibrarySystemEntities()) { return context.Books.ToList(); } } catch (Exception) { throw; } }
protected void Page_Load(object sender, EventArgs e) { int bookId = Convert.ToInt32(Request.QueryString["id"]); if (bookId == 0) { Response.Redirect("~/Home.aspx"); } LibrarySystemEntities context = new LibrarySystemEntities(); // bind book var book = context.Books.Where(u => u.BookId == bookId).ToList(); this.ListViewBookDetails.DataSource = book; this.ListViewBookDetails.DataBind(); }
protected void Page_Load(object sender, EventArgs e) { var context = new LibrarySystemEntities(); int bookId = int.Parse(Request.Params["bookId"]); Book getBook = context.Books.FirstOrDefault(b => b.Id == bookId); this.BookTitle.Text = getBook.Title; this.BookAuthor.Text = getBook.Author; this.BookISBN.Text = getBook.ISBN; this.BookWebSite.Text = Server.HtmlEncode(getBook.WebSite); this.BookWebSite.NavigateUrl = getBook.WebSite; this.BookDescription.Text = getBook.Description; }
protected void AllCategories_SelectedIndexChanged(object sender, EventArgs e) { this.deleteCategoryConteiner.Visible = false; this.createCategoryConteiner.Visible = false; this.editCategoryConteiner.Visible = true; int categoryId = Convert.ToInt32(this.AllCategories.SelectedDataKey.Value); var context = new LibrarySystemEntities(); var category = context.Categories.Find(categoryId); this.EditCategoryButton.CommandArgument = category.Id.ToString(); this.NameOfEditCategory.Text = "Edit " + category.Title + " category"; this.TexboxForCategoryToEdit.Text = category.Title; }
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(); } }
protected void EditBookButton_Command(object sender, CommandEventArgs e) { var bookId = e.CommandArgument; if (bookId == null) { Error_Handler_Control.ErrorSuccessNotifier .AddErrorMessage("Selected book couldn't be opened for editing."); return; } this.BookId = Convert.ToInt32(bookId); using (var dbContext = new LibrarySystemEntities()) { try { var book = dbContext.Books.Find(this.BookId); var selectedBookCategory = book.CategoryId.ToString(); var categories = dbContext.Categories.ToList(); this.EditBookCategoryDropDown.DataSource = categories; this.EditBookCategoryDropDown.DataBind(); this.EditBookCategoryDropDown.SelectedValue = selectedBookCategory; this.EditBookTitle.Text = book.Title; this.EditBookAuthor.Text = book.Author; this.EditBookISBN.Text = book.ISBN; this.EditBookUrl.Text = book.Url; this.EditBookDescription.InnerText = book.Description; } catch (EntityDataSourceValidationException ex) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage(ex); return; } catch { Error_Handler_Control.ErrorSuccessNotifier .AddErrorMessage("The database connection was lost. Try again later."); return; } } this.EditBookPanel.Visible = true; }
public Book AddBook(Book _book) { try { using (var context = new LibrarySystemEntities()) { context.Books.Add(_book); context.SaveChanges(); return _book; } } catch (Exception) { throw; } }
// The return type can be changed to IEnumerable, however to support // paging and sorting, the following parameters must be added: // int maximumRows // int startRowIndex // out int totalRowCount // string sortByExpression public Book BookDetailsListView_GetData() { int id=0; try { id = int.Parse(Request.Params["id"]); } catch (Exception) { ErrorSuccessNotifier.AddErrorMessage("Invalid Id argument"); ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("Default.aspx"); } var context = new LibrarySystemEntities(); var book = context.Books.Find(id); return book; }
// The return type can be changed to IEnumerable, however to support // paging and sorting, the following parameters must be added: // int maximumRows // int startRowIndex // out int totalRowCount // string sortByExpression public IQueryable<Book> SearchResultsListView_GetData() { var context = new LibrarySystemEntities(); var query = Request.Params["q"]; if (query.Length > 0) { var books = context.Books .Where(book => book.Title.ToLower().Contains(query.ToLower()) || book.Author.ToLower().Contains(query.ToLower())); return books.OrderBy(b=>b.Title).ThenBy(b=>b.Author); } else { var books = context.Books; return books.OrderBy(b => b.Title).ThenBy(b => b.Author); } }
protected void Page_Load(object sender, EventArgs e) { var context = new LibrarySystemEntities(); var text = this.Request.Params["q"]; var books = context.Books.ToList(); if (!string.IsNullOrWhiteSpace(text)) { this.IQuery.InnerText = text; books = books.Where(b => b.Title.ToLower().Contains(text.ToLower()) || b.Author.ToLower().Contains(text.ToLower())).ToList(); } this.ListViewSearchResult.DataSource = books.OrderBy(b => b.Title).ThenBy(b => b.Author); this.ListViewSearchResult.DataBind(); }
protected void OnButtonShowDeleteBookPanel_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) { this.ShowDeleteBookPanel(); this.ButtonDeleteBook.CommandArgument = bookId.ToString(); this.TextBoxDeleteBookTitle.Text = book.Title; } else { ErrorSuccessNotifier.AddErrorMessage("This book does not exist anymore"); } } }
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 Page_Load(object sender, EventArgs e) { var context = new LibrarySystemEntities(); try { var query = this.Request.Params["q"].ToString().Trim(); var queryResults = context.Books.Include(b => b.Category) .Where(b => b.Title.Contains(query) || b.Authors.Contains(query)) .OrderBy(b => b.Title).ThenBy(b => b.Authors); this.RepeaterQueryResults.DataSource = queryResults.ToList(); this.RepeaterQueryResults.DataBind(); this.LiteralQuery.Text = this.Server.HtmlEncode(query); this.HyperLinkBackToBooks.NavigateUrl = "Default.aspx"; } catch (Exception exc) { ErrorSuccessNotifier.AddErrorMessage(exc); } }
public Book GetBookById(int _id) { try { using (var contex = new LibrarySystemEntities()) { contex.Configuration.LazyLoadingEnabled = false; Book book = contex.Books.FirstOrDefault(c => c.BookId == _id); return book; } } catch (Exception) { throw; } }
protected void OnButtonShowEditCategory_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) { this.PanelCreateCategory.Visible = false; this.PanelDeleteCategory.Visible = false; this.PanelEditCategory.Visible = true; this.TextBoxEditCategoryName.Text = category.Name; this.ButtonSaveCategory.CommandArgument = categoryId.ToString(); } else { ErrorSuccessNotifier.AddErrorMessage("This category does not exist anymore"); } } }
protected void EditButton_Command(object sender, CommandEventArgs e) { if (e.CommandArgument == null) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("No selected category"); return; } var id = Convert.ToInt32(e.CommandArgument); using (var dbContext = new LibrarySystemEntities()) { var category = dbContext.Categories.Find(id); if (category == null) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("This category was not found!"); return; } this.CategoryEditTextBox.Text = category.Name; this.CategoryId = category.Id; this.EditCategoryPanel.Visible = true; } }
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 Page_Load(object sender, EventArgs e) { var bookIdString = Request.Params["id"]; int bookId; if (int.TryParse(bookIdString, out bookId) && bookId > 0) { LibrarySystemEntities context = new LibrarySystemEntities(); var book = context.Books.Where(b => b.Id == bookId).ToList(); if (book != null) { this.FormViewBooks.DataSource = book; this.FormViewBooks.DataBind(); } else { Response.Redirect("~/"); } } else { Response.Redirect("~/"); } }
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; }
protected void Page_PreRender(object sender, EventArgs e) { try { LibrarySystemEntities db = new LibrarySystemEntities(); using (db) { var cats = db.Categories.ToList(); var html = new StringBuilder(); html.Append("<div class='span11'>"); foreach (var cat in cats) { html.Append("<h2>"); html.Append(Server.HtmlDecode(cat.name)); html.Append("</h2>"); html.Append("<ul>"); if (cat.Books.Count > 0) { foreach (var book in cat.Books) { html.Append("<li>"); html.Append("<a href='BookDetails.aspx?id=" + Server.HtmlDecode(book.id.ToString()) + "'>"); html.Append(Server.HtmlDecode(book.title) + " by " + Server.HtmlDecode(book.author)); html.Append("</a>"); html.Append("</li>"); } } else { html.Append("<p class='no-books'>No books in this category.</p>"); } html.Append("</ul>"); } html.Append("</div>"); this.content.InnerHtml = html.ToString(); } } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. ErrorSuccessNotifier.AddErrorMessage(exceptionMessage + " " + ex.EntityValidationErrors); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } }
protected void LinkButtonShowEditPanel_Command(object sender, CommandEventArgs e) { if (e.CommandName == CommandEditCategory) { int categoryId = Convert.ToInt32(e.CommandArgument.ToString()); this.HiddenField.Text = categoryId.ToString(); LibrarySystemEntities context = new LibrarySystemEntities(); Category category = context.Categories.Find(categoryId); this.TextBoxEditedCategoryName.Text = category.Name; } this.LinkButtonShowCreatePanel.Visible = false; this.PanelCreateCategory.Visible = false; this.PanelEditCategory.Visible = true; }
protected void Page_PreRender(object sender, EventArgs e) { LibrarySystemEntities context = new LibrarySystemEntities(); // bind books var books = context.Books.ToList(); this.GridViewAllBooks.DataSource = books; this.GridViewAllBooks.DataBind(); // bind categories var categories = context.Categories.ToList(); this.DropDownListCategories.DataSource = categories; this.DropDownListCategories.DataBind(); this.DropDownListEditedCategories.DataSource = categories; this.DropDownListEditedCategories.DataBind(); }
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; }
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 Page_Load(object sender, EventArgs e) { LibrarySystemEntities context = new LibrarySystemEntities(); this.ListViewCategories.DataSource = context.Categories.ToList(); this.ListViewCategories.DataBind(); }
public IQueryable<BookModel> booksListView_GetData() { try { var q = Request.QueryString["q"].ToLower(); LibrarySystemEntities db = new LibrarySystemEntities(); if (q.Length >= 20000) { throw new ArgumentOutOfRangeException("The search phrase is too long. It must be less than 20000 characters."); } if (q == string.Empty) { var books = (from b in db.Books.ToList() select new BookModel() { Id = b.id, Title = b.title, Author = b.author, Description = b.description, Isbn = b.isbn, WebSite = b.webSite, Category = db.Categories.Find(b.categoryId).name }); return books.OrderBy(b => b.Title).ThenBy(b => b.Author).AsQueryable<BookModel>(); } else { var books = (from b in db.Books.ToList() where (b.author.ToLower().Contains(q) || b.title.ToLower().Contains(q)) select new BookModel() { Id = b.id, Title = b.title, Author = b.author, Description = b.description, Isbn = b.isbn, WebSite = b.webSite, Category = db.Categories.Find(b.categoryId).name }); return books.OrderBy(b => b.Title).ThenBy(b => b.Author).AsQueryable<BookModel>(); } } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. ErrorSuccessNotifier.AddErrorMessage(exceptionMessage + " " + ex.EntityValidationErrors); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } return null; }
protected void LinkButtonShowEditPanel_Command(object sender, CommandEventArgs e) { if (e.CommandName == CommandEditBook) { int bookId = Convert.ToInt32(e.CommandArgument.ToString()); this.HiddenField.Text = bookId.ToString(); LibrarySystemEntities context = new LibrarySystemEntities(); Book book = context.Books.Find(bookId); this.TextBoxEditedTitle.Text = book.Title; this.TextBoxEditedAuthor.Text = book.Author; this.TextBoxEditedISBN.Text = book.ISBN; this.TextBoxEditedWebSite.Text = book.ISBN; this.TextBoxEditedDescription.Text = book.Description; this.DropDownListEditedCategories.Text = book.Category.Name; } this.LinkButtonShowCreatePanel.Visible = false; this.PanelCreateBook.Visible = false; this.PanelEditBook.Visible = true; }
protected void Page_PreRender(object sender, EventArgs e) { LibrarySystemEntities context = new LibrarySystemEntities(); var categories = context.Categories.ToList(); this.GridViewAllCategories.DataSource = categories; this.GridViewAllCategories.DataBind(); }