// 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);
            }
        }
        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);
            }
        }