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 Page_Load(object sender, EventArgs e)
 {
     string text = Request.Params["q"];
     this.HeaderSearchPage.InnerText = String.Format("Search Results for Query \"{0}\":", text);
     BookLibraryEntities db = new BookLibraryEntities();
     var books = db.Books.Where(b => b.Title.Contains(text));
     this.RepeaterBooks.DataSource = books.ToList();
     this.RepeaterBooks.DataBind();
 }
 protected void LinkButtonCreateNewBook_Click(object sender, EventArgs e)
 {
     BookLibraryEntities db = new BookLibraryEntities();
     this.TitleCreateOrEdit.InnerText = "Create New Book";
     this.LinkButtonCreateOrEdit.Text = "Create";
     this.TextBoxTitle.Text = "";
     this.TextBoxAuthors.Text = "";
     this.TextBoxISBN.Text = "";
     this.TextBoxWebsite.Text = "";
     this.TextBoxDescription.Text = "";
     this.DropDownListCategories.DataSource = db.Categories.ToList();
     this.PanelCreateOrEdit.Visible = true;
 }
 protected void Page_Load(object sender, EventArgs e)
 {
     int bookId = Convert.ToInt32(Request.Params["id"]);
     if (bookId > 0)
     {
         BookLibraryEntities db = new BookLibraryEntities();
         var currentBook = db.Books.Where(b => b.Id == bookId).ToList(); ;
         this.FormViewBook.DataSource = currentBook;
         this.DataBind();
         //this.LiteralTitle.Text = currentBook.Title;
         //this.LiteralAuthor.Text = currentBook.Author;
         //this.HyperLinkWebsite.Text = currentBook.Website;
         //this.LiteralDescription.Text = currentBook.Description;
     }
 }
 // The id parameter name should match the DataKeyNames value set on the control
 public void GridViewCategories_DeleteItem(int id)
 {
     BookLibraryEntities db = new BookLibraryEntities();
     Book book = db.Books.FirstOrDefault(c => c.Id == id);
     try
     {
         db.Books.Remove(book);
         db.SaveChanges();
         this.GridViewBooks.PageIndex = 0;
         ErrorSuccessNotifier.AddInfoMessage("Book successfully deleted!");
     }
     catch(Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage(ex);
     }
 }
        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);
            }
        }
 // 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<BookLibrary.Models.Book> GridViewCategories_GetData()
 {
     BookLibraryEntities db = new BookLibraryEntities();
     return db.Books;
 }
        protected void LinkButtonEditCategory_Command(object sender, CommandEventArgs e)
        {
            int bookId = Convert.ToInt32(e.CommandArgument);

            BookLibraryEntities db = new BookLibraryEntities();
            Book book = db.Books.FirstOrDefault(b => b.Id == bookId);

            if (book != null)
            {
                this.LabelBookId.Text = bookId.ToString();
                this.TitleCreateOrEdit.InnerText = "Edit Book";
                this.TextBoxTitle.Text = book.Title;
                this.TextBoxAuthors.Text = book.Author;
                this.TextBoxISBN.Text = book.ISBN;
                this.TextBoxWebsite.Text = book.Website;
                this.TextBoxDescription.Text = book.Description;
                this.DropDownListCategories.DataSource = db.Categories.ToList();
                this.LinkButtonCreateOrEdit.Text = "Save";
                this.PanelCreateOrEdit.Visible = true;
            }
        }
        protected void LinkButtonEditCategory_Command(object sender, CommandEventArgs e)
        {
            int categoryId = Convert.ToInt32(e.CommandArgument);

            BookLibraryEntities db = new BookLibraryEntities();
            Category category = db.Categories.FirstOrDefault(c => c.Id == categoryId);

            if (category != null)
            {
                this.LabelCategoryId.Text = categoryId.ToString();
                this.TitleCreateOrEdit.InnerText = "Edit Category";
                this.TextBoxCreateOrEdit.Text = category.Name;
                this.LinkButtonCreateOrEdit.Text = "Save";
                this.PanelCreateOrEdit.Visible = true;
            }
        }
 // 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<BookLibrary.Models.Category> ListViewCategories_GetData()
 {
     BookLibraryEntities db = new BookLibraryEntities();
     return db.Categories;
 }