// 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> GridViewEditCategories_GetData()
        {
            ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
            var categories = context.Categories.OrderBy(cat => cat.CategoryId);

            return categories;
        }
        public IQueryable<Category> LoadDropDown()
        {
            ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
            var categories = context.Categories.OrderBy(cat => cat.CategoryId);

            return categories;
        }
        // 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<ExamWebFormsASP.Models.Category> ListViewAllBooks_GetData()
        {
            ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
            var categories = context.Categories.Include("Books");

            return categories;
        }
        // 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> GridViewEditBooks_GetData()
        {
            ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
            var books = context.Books.OrderBy(b => b.BookId);

            return books;
        }
        // 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 ListViewBookDetails_GetData()
        {
            int id = Convert.ToInt32(Request.Params["id"]);
            ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
            var book = context.Books.Find(id);

            return book;
        }
 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 ShowEditMenu(object sender, EventArgs e)
        {
            LinkButton lnkEdit = (LinkButton)sender;
            var categoryId = Convert.ToInt32(lnkEdit.CommandArgument);
            ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
            var category = context.Categories.Find(categoryId);
            var categDatasource = new List<Category> { category };

            this.ListViewEditCategory.DataSource = categDatasource;
            this.ListViewEditCategory.DataBind();
        }
 protected void ShowEditMenu(object sender, EventArgs e)
 {
     LinkButton lnkEdit = (LinkButton)sender;
     var bookId = Convert.ToInt32(lnkEdit.CommandArgument);
     ExamWebFormsASPEntities1 context = new ExamWebFormsASPEntities1();
     var book = context.Books.Find(bookId);
     var bookDatasource = new List<Book> { book };
     
     this.ListViewEditBook.DataSource = bookDatasource;
     this.ListViewEditBook.DataBind();
 }
        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);
                }
                
            }
        }
        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);
         }
     }
 }