protected void CreateNewCategory(object sender, EventArgs e)
 {
     var newCategory = this.TextBoxEditCategory.Text;
     Category category = new Category() { CategoryName = newCategory };
     ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
     context.Categories.Add(category);
     context.SaveChanges();
 }
        protected void DeleteBook(object sender, EventArgs e)
        {
            LinkButton lnkSave = (LinkButton)sender;
            var bookId = Convert.ToInt32(lnkSave.CommandArgument);
            ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
            var book = context.Books.Find(bookId);

            using (context)
            {
                context.Books.Remove(book);
                context.SaveChanges();
                this.GridViewEditBooks.PageIndex = 0;
            }
            
        }
        protected void EditCategory(object sender, EventArgs e)
        {
            LinkButton lnkSave = (LinkButton)sender;
            var categoryId = Convert.ToInt32(lnkSave.CommandArgument);
            ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
            using (context)
            {
                var category = context.Categories.Find(categoryId);

                var ctrl = (Control)sender;
                var lvi = (ListViewItem)ctrl.NamingContainer;
                var txt = (TextBox)lvi.FindControl("TextBoxEditCategory");

                var newCat = txt.Text;
                category.CategoryName = newCat;
                context.SaveChanges();
                this.GridViewEditCategories.DataBind();
            }
        }
 protected void DeleteCategory(object sender, EventArgs e)
 {
     LinkButton lnkDelete = (LinkButton)sender;
     var categoryId = Convert.ToInt32(lnkDelete.CommandArgument);
     ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
     var category = context.Categories.Include("Books").
         FirstOrDefault(c => c.CategoryId == categoryId);
     
     using (context)
     {
         try
         {
             context.Books.RemoveRange(category.Books);
             context.Categories.Remove(category);
             context.SaveChanges();
             this.GridViewEditCategories.PageIndex = 0;
             ErrorSuccessNotifier.AddInfoMessage("Question successfully deleted.");
         }
         catch (Exception ex)
         {
             ErrorSuccessNotifier.AddErrorMessage(ex);
         }
     }
 }
        protected void EditBook(object sender, EventArgs e)
        {
            LinkButton lnkSave = (LinkButton)sender;
            var bookId = Convert.ToInt32(lnkSave.CommandArgument);
            ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
            var book = context.Books.Find(bookId);
            using (context)
            {
                try
                {
                    var ctrl = (Control)sender;
                    var lvi = (ListViewItem)ctrl.NamingContainer;

                    var txtTitle = (TextBox)lvi.FindControl("TextBoxEditTitle");
                    var newTitle = txtTitle.Text;
                    book.Title = newTitle;

                    var txtAuthors = (TextBox)lvi.FindControl("TextBoxEditAuthors");
                    var newAuthors = txtAuthors.Text;
                    book.Author = newAuthors;

                    var txtIsbn = (TextBox)lvi.FindControl("TextBoxIsbn");
                    var newIsbn = txtIsbn.Text;
                    book.ISBN = newIsbn;

                    var txtWebSite = (TextBox)lvi.FindControl("TextBoxWebSite");
                    var newWeSite = txtWebSite.Text;
                    book.Url = newWeSite;

                    var txtDescription = (TextBox)lvi.FindControl("TextBoxtDescription");
                    var newDescription = txtDescription.Text;
                    book.Description = newDescription;

                    var txtCategory = (DropDownList)lvi.FindControl("DropDownCategory");
                    var newCategoryName = txtCategory.SelectedItem.Text;
                    Category newCategory = context.Categories.Where(c => c.CategoryName == newCategoryName).FirstOrDefault();
                    book.Category = newCategory;

                    context.SaveChanges();
                    ErrorSuccessNotifier.AddInfoMessage("Book successfully edited");
                    this.GridViewEditBooks.DataBind();
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
                
            }
        }