コード例 #1
0
        protected void CreateCategoryButton_Command(object sender, CommandEventArgs e)
        {
            var context = new LibrarySystemEntities();

            string categoryName = this.CreateCategoryField.Text;

            if (string.IsNullOrWhiteSpace(categoryName))
            {
                ErrorSuccessNotifier.AddErrorMessage("Category name cannot be empty");
                this.Response.Redirect("~/Admin/EditCategories.aspx");
            }

            if (categoryName.Length < 3 || categoryName.Length > 100)
            {
                ErrorSuccessNotifier.AddErrorMessage("Category length should be  between 3 and 100 symbols");
                this.Response.Redirect("~/Admin/EditCategories.aspx");
            }

            Category newCategory = new Category();
            newCategory.CategoryName = categoryName;

            context.Categories.Add(newCategory);
            context.SaveChanges();

            ErrorSuccessNotifier.AddSuccessMessage("Category created!");
            this.Response.Redirect("~/Admin/EditCategories.aspx");
        }
コード例 #2
0
        protected void Yes_Click(object sender, EventArgs e)
        {
            var context = new LibrarySystemEntities();

            Button but = (Button)sender;
            int id = int.Parse(but.CommandArgument);


            var category = context.Categories.Find(id);

            context.Categories.Remove(category);

            try
            {
                context.SaveChanges();
                this.EditCategoriesList.DataBind();
                this.DeleteForm.Visible = false;
                ErrorSuccessNotifier.AddSuccessMessage("Category Deleted");
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
                ErrorSuccessNotifier.ShowAfterRedirect = true;
                Response.Redirect("../EditCategories.aspx");

            }
        }
コード例 #3
0
        protected void SaveBtn_Click(object sender, CommandEventArgs e)
        {
            try
            {
                var id = Convert.ToInt32(e.CommandArgument);

                LibrarySystemEntities db = new LibrarySystemEntities();
                var currCat = db.Categories.FirstOrDefault(c => c.id == id);

                currCat.name = catName.Text;
                db.SaveChanges();
                Response.Redirect("~/Admin/EditCategories.aspx");
            }
            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);
            }
        }
コード例 #4
0
        protected void DeleteCategoryButton_Command(object sender, CommandEventArgs e)
        {
            var context = new LibrarySystemEntities();

            int categoryId = int.Parse(e.CommandArgument.ToString());
            var category = context.Categories.FirstOrDefault(c => c.Id == categoryId);

            var currentCategoryBooks = category.Books;

            context.Books.RemoveRange(currentCategoryBooks);

            context.Categories.Remove(category);
            context.SaveChanges();

            ErrorSuccessNotifier.AddSuccessMessage("Category deleted!");
            this.Response.Redirect("~/Admin/EditCategories.aspx");
        }
コード例 #5
0
 protected void LinkButtonDoDelete_Click(object sender, EventArgs e)
 {
     try
     {
         LibrarySystemEntities context = new LibrarySystemEntities();
         var bookId = Convert.ToInt32(this.HiddenFieldDeleteBookId.Value);
         var book = context.Books.Find(bookId);
         context.Books.Remove(book);
         context.SaveChanges();
         HideAllPanels();
         this.GridViewBooks.DataBind();
         ErrorSuccessNotifier.AddSuccessMessage("Book deleted.");
     }
     catch (Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage(ex);
     }
 }
コード例 #6
0
 protected void LinkButtonDoDelete_Click(object sender, EventArgs e)
 {
     try
     {
         LibrarySystemEntities context = new LibrarySystemEntities();
         var catId = Convert.ToInt32(this.HiddenFieldDeleteCategoryId.Value);
         var cat = context.Categories.Find(catId);
         context.Books.RemoveRange(cat.Books); // Remove all books for this caregory
         context.Categories.Remove(cat);
         context.SaveChanges();
         HideAllPanels();
         this.GridViewCategories.DataBind();
         ErrorSuccessNotifier.AddSuccessMessage("Category deleted.");
     }
     catch (Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage(ex);
     }
 }
コード例 #7
0
        protected void SaveBtn_Click(object sender, CommandEventArgs e)
        {
            try
            {
                var id = Convert.ToInt32(e.CommandArgument);

                LibrarySystemEntities db = new LibrarySystemEntities();
                var currBook = db.Books.FirstOrDefault(b => b.id == id);

                currBook.title = bookTitle.Text;
                currBook.author = bookAuthor.Text;
                currBook.isbn = bookIsbn.Text;
                currBook.webSite = bookWebSite.Text;
                currBook.description = bookDescr.Text;
                currBook.categoryId = db.Categories.First(b => b.name == bookCats.SelectedItem.Value).id;
                db.SaveChanges();
                Response.Redirect("~/Admin/EditBooks.aspx");
            }
            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);
            }
        }
コード例 #8
0
 protected void LinkButtonSaveCategory_Click(object sender, EventArgs e)
 {
     try
     {
         LibrarySystemEntities context = new LibrarySystemEntities();
         Category cat = new Category();
         cat.Title = this.TextBoxCategoryName.Text;
         context.Categories.Add(cat);
         context.SaveChanges();
         HideAllPanels();
         this.GridViewCategories.DataBind();
         ErrorSuccessNotifier.AddSuccessMessage("Category created.");
     }
     catch (Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage(ex);
     }
 }
コード例 #9
0
        protected void SaveEdit_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                string title = this.EditBookTitleTb.Text;
                string author = this.EditBookAuthorTb.Text;
                string isbn = this.EditBookIsbnTb.Text;
                string webSite = this.EditWebSiteTb.Text;
                string description = this.EditDescriptionTa.Value;
                int categoryId = int.Parse(this.EditAllCategories.SelectedValue);

                Button but = (Button)sender;
                int id = int.Parse(but.CommandArgument);

                var context = new LibrarySystemEntities();

                var book = context.Books.Find(id);

                book.Title = title;
                book.Author = author;
                book.ISBN = isbn;
                book.WebSite = webSite;
                book.Description = description;
                book.CategoryId = categoryId;

                try
                {
                    context.SaveChanges();
                    this.EditBookTitleTb.Text = "";
                    this.EditBookAuthorTb.Text = "";
                    this.EditBookIsbnTb.Text ="";
                    this.EditWebSiteTb.Text = "";
                    this.EditDescriptionTa.Value = "";
                    this.EditForm.Visible = false;
                    this.BooksListView.DataBind();

                    ErrorSuccessNotifier.AddSuccessMessage("Book Edited");
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    Response.Redirect("../EditCategories.aspx");

                }
            }
        }
コード例 #10
0
        protected void CreateBtn_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var context = new LibrarySystemEntities();

                var title = this.BookTitleTb.Text;
                var author = this.BookAuthorTb.Text;
                var isbn = this.BookISBNTb.Text;
                var webSite = this.BookWebSite.Text;
                var description = this.BookDescriptionTa.Value;
                int categoryId = int.Parse(this.AllCategoriesList.SelectedValue);

                Book book = new Book()
                {
                    Title = title,
                    Author = author,
                    ISBN = isbn,
                    WebSite = webSite,
                    Description = description,
                    CategoryId = categoryId
                };

                try
                {
                    context.Books.Add(book);
                    context.SaveChanges();
                    this.BooksListView.DataBind();
                    this.InsertForm.Visible = false;

                    this.BookTitleTb.Text = "";
                    this.BookAuthorTb.Text = "";
                    this.BookISBNTb.Text = "";
                    this.BookWebSite.Text = "";
                    this.BookDescriptionTa.Value = "";


                    ErrorSuccessNotifier.AddSuccessMessage("Book Added");
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    Response.Redirect("../EditCategories.aspx");

                }
            }

        }
コード例 #11
0
        protected void CreateBtn_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var context = new LibrarySystemEntities();
                string title = this.CategoryTitleTb.Text;
                Category category = new Category()
                {
                    Name = title
                };

                try
                {
                    context.Categories.Add(category);
                    context.SaveChanges();
                    this.CategoryTitleTb.Text = "";
                    this.InsertForm.Visible = false;
                    this.EditCategoriesList.DataBind();

                    ErrorSuccessNotifier.AddSuccessMessage("Category created");
                }
                catch (Exception ex)
                {

                    ErrorSuccessNotifier.AddErrorMessage(ex);
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    Response.Redirect("../EditCategories.aspx");
                }
            }

        }
コード例 #12
0
        protected void EditCategoryButton_Command(object sender, CommandEventArgs e)
        {
            var context = new LibrarySystemEntities();

            string newCategoryName = this.EditCategoryField.Text;

            if (string.IsNullOrWhiteSpace(newCategoryName))
            {
                ErrorSuccessNotifier.AddErrorMessage("Category name cannot be empty");
                this.Response.Redirect("~/Admin/EditCategories.aspx");
            }

            if (newCategoryName.Length < 3 || newCategoryName.Length > 100)
            {
                ErrorSuccessNotifier.AddErrorMessage("Category length should be between 3 and 100 symbols");
                this.Response.Redirect("~/Admin/EditCategories.aspx");
            }

            int categoryId = int.Parse(e.CommandArgument.ToString());
            var category = context.Categories.FirstOrDefault(c => c.Id == categoryId);

            category.CategoryName = newCategoryName;
            context.SaveChanges();

            ErrorSuccessNotifier.AddSuccessMessage("Category modified!");
            this.Response.Redirect("~/Admin/EditCategories.aspx");
        }
コード例 #13
0
        protected void LinkButtonCreateConfirm_Click(object sender, EventArgs e)
        {
            var categoryName = this.TextBoxCreateCategoryName.Text.Trim();
            if (categoryName.Length < 2)
            {
                ErrorSuccessNotifier.AddErrorMessage("Category name must consist of at least 2 symbols!");
                return;
            }

            var context = new LibrarySystemEntities();
            if (context.Categories.Any(c => c.Name == categoryName))
            {
                ErrorSuccessNotifier.AddErrorMessage("Category with this name already exists!");
                return;
            }

            var newCategory = new Category();
            newCategory.Name = categoryName;
            context.Categories.Add(newCategory);

            try
            {
                context.SaveChanges();
                this.GridViewCategories.DataBind();
                this.TextBoxCreateCategoryName.Text = string.Empty;
                ErrorSuccessNotifier.AddSuccessMessage("Category successfully created!");
            }
            catch (Exception exc)
            {
                ErrorSuccessNotifier.AddErrorMessage(exc);
            }
        }
コード例 #14
0
        protected void CreateBookButton_Command(object sender, CommandEventArgs e)
        {
            var context = new LibrarySystemEntities();

            string title = this.TextBoxTitle.Text;
            string author = this.TextBoxAuthor.Text;
            string isbn = this.TextBoxISBN.Text;
            string webSite = this.TextBoxWebSite.Text;
            string description = this.TextBoxDescription.Text;
            int categoryIndex = int.Parse(this.DropDownListCategory.SelectedValue);

            if (string.IsNullOrWhiteSpace(title))
            {
                ErrorSuccessNotifier.AddErrorMessage("Title cannot be empty");
                this.Response.Redirect("~/Admin/EditCategories.aspx");
            }

            if (string.IsNullOrWhiteSpace(author))
            {
                ErrorSuccessNotifier.AddErrorMessage("Author cannot be empty");
                this.Response.Redirect("~/Admin/EditCategories.aspx");
            }

            if (title.Length < 3 || title.Length > 256)
            {
                ErrorSuccessNotifier.AddErrorMessage("Title's length should be between 3 and 256 symbols");
                this.Response.Redirect("~/Admin/EditBooks.aspx");
            }

            if (author.Length < 3 || author.Length > 100)
            {
                ErrorSuccessNotifier.AddErrorMessage("Author's length should be between 3 and 100 symbols");
                this.Response.Redirect("~/Admin/EditBooks.aspx");
            }

            var category = context.Categories.FirstOrDefault(c => c.Id == categoryIndex);

            Book newBook = new Book();
            newBook.Title = title;
            newBook.Author = author;
            newBook.ISBN = isbn;
            newBook.WebSite = webSite;
            newBook.Description = description;
            newBook.Category = category;

            category.Books.Add(newBook);

            context.SaveChanges();

            ErrorSuccessNotifier.AddSuccessMessage("Book created!");
            this.Response.Redirect("~/Admin/EditBooks.aspx");
        }
コード例 #15
0
 protected void LinkButtonEditSave_Click(object sender, EventArgs e)
 {
     try
     {
         LibrarySystemEntities context = new LibrarySystemEntities();
         var catId = Convert.ToInt32(this.HiddenFieldEditCategoryId.Value);
         var cat = context.Categories.Find(catId);
         cat.Title = this.TextBoxEditCategoryName.Text;
         context.SaveChanges();
         HideAllPanels();
         this.GridViewCategories.DataBind();
         ErrorSuccessNotifier.AddSuccessMessage("Category modified.");
     }
     catch (Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage(ex);
     }
 }
コード例 #16
0
        protected void CreateCat_Click(object sender, EventArgs e)
        {
            try
            {
                LibrarySystemEntities db = new LibrarySystemEntities();
                db.Categories.Add(new Categories()
                {
                    name = newCatName.Text
                });

                db.SaveChanges();
                Response.Redirect("~/Admin/EditCategories.aspx");
            }
            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);
            }
        }
コード例 #17
0
        protected void LinkButtonDeleteConfirm_Click(object sender, EventArgs e)
        {
            var bookToDeleteId = Convert.ToInt32(this.LiteralBookToDeleteId.Text);
            var context = new LibrarySystemEntities();

            var bookToDelete = context.Books.FirstOrDefault(b => b.BookId == bookToDeleteId);
            if (bookToDelete == null)
            {
                ErrorSuccessNotifier.AddErrorMessage(
                    "An unexpected error occured! The book you want to delete was not found...");
            }
            else
            {
                try
                {
                    context.Books.Remove(bookToDelete);
                    context.SaveChanges();
                    this.GridViewBooks.PageIndex = 0;
                    this.GridViewBooks.DataBind();
                    ErrorSuccessNotifier.AddSuccessMessage("Book successfully deleted!");
                }
                catch (Exception exc)
                {
                    ErrorSuccessNotifier.AddErrorMessage(exc);
                }
            }
        }
コード例 #18
0
        protected void LinkButtonEditSave_Click(object sender, EventArgs e)
        {
            var bookToEditId = Convert.ToInt32(this.LiteralBookToEditId.Text);
            var context = new LibrarySystemEntities();
            var bookToEdit = context.Books.FirstOrDefault(b => b.BookId == bookToEditId);
            if (bookToEdit == null)
            {
                ErrorSuccessNotifier.AddErrorMessage(
                    "An unexpected error occurred! The book you want to edit was not found...");
                return;
            }

            bookToEdit = this.LoadBookData(bookToEdit, context);
            if (bookToEdit == null)
            {
                return;
            }

            try
            {
                context.SaveChanges();
                this.GridViewBooks.DataBind();
                this.TextBoxCreateEditBookAuthors.Text = string.Empty;
                this.TextBoxCreateEditBookDescription.Text = string.Empty;
                this.TextBoxCreateEditBookIsbn.Text = string.Empty;
                this.TextBoxCreateEditBookTitle.Text = string.Empty;
                this.TextBoxCreateEditBookWebSite.Text = string.Empty;
                ErrorSuccessNotifier.AddSuccessMessage("Book successfully edited!");
            }
            catch (Exception exc)
            {
                ErrorSuccessNotifier.AddErrorMessage(exc);
            }
        }
コード例 #19
0
        protected void LinkButtonCreateConfirm_Click(object sender, EventArgs e)
        {
            var context = new LibrarySystemEntities();
            var newBook = new Book();
            newBook = this.LoadBookData(newBook, context);
            if (newBook == null)
            {
                return;
            }

            try
            {
                context.Books.Add(newBook);
                context.SaveChanges();
                this.GridViewBooks.DataBind();
                this.TextBoxCreateEditBookAuthors.Text = string.Empty;
                this.TextBoxCreateEditBookDescription.Text = string.Empty;
                this.TextBoxCreateEditBookIsbn.Text = string.Empty;
                this.TextBoxCreateEditBookTitle.Text = string.Empty;
                this.TextBoxCreateEditBookWebSite.Text = string.Empty;
                ErrorSuccessNotifier.AddSuccessMessage("Book successfully created!");
            }
            catch (Exception exc)
            {
                ErrorSuccessNotifier.AddErrorMessage(exc);
            }
        }
コード例 #20
0
        protected void CreateBook_Click(object sender, EventArgs e)
        {
            try
            {
                LibrarySystemEntities db = new LibrarySystemEntities();
                bookNewCat.DataSource = db.Categories.ToList();
                bookNewCat.DataBind();

                db.Books.Add(new Books()
                {
                    title = bookNewTitle.Text,
                    author = bookNewAuthor.Text,
                    isbn = bookNewIsbn.Text,
                    description = bookNewDescr.Text,
                    webSite = bookNewWebSite.Text,
                    categoryId = db.Categories.First(b => b.name == bookNewCat.SelectedItem.Value).id
                });

                db.SaveChanges();
                Response.Redirect("~/Admin/EditBooks.aspx");
            }
            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);
            }
        }
コード例 #21
0
        protected void LinkButtonSaveBook_Click(object sender, EventArgs e)
        {
            try
            {
                LibrarySystemEntities context = new LibrarySystemEntities();
                Book book = new Book();
                book.Title = this.TextBoxCreateBookTitle.Text;
                book.Author = this.TextBoxCreateBookAuthor.Text;
                book.ISBN = this.TextBoxCreateBookISBN.Text;
                book.WebSite = this.TextBoxCreateBookWebSite.Text;
                book.Description = this.TextBoxCreateBookDescription.Text;
                book.CategoryId = Convert.ToInt32(this.DropDownListCreateBookCategory.SelectedValue);
                context.Books.Add(book);
                context.SaveChanges();
                HideAllPanels();
                this.GridViewBooks.DataBind();
                HideAllPanels();

                ErrorSuccessNotifier.AddSuccessMessage("Book created.");
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
コード例 #22
0
        protected void EditBookButton_Command(object sender, CommandEventArgs e)
        {
            var context = new LibrarySystemEntities();

            string title = this.TextBoxEditTitle.Text;
            string author = this.TextBoxEditAuthor.Text;
            string isbn = this.TextBoxEditISBN.Text;
            string webSite = this.TextBoxEditWebSite.Text;
            string description = this.TextBoxEditDescription.Text;
            int categoryIndex = int.Parse(this.DropDownListEditCategory.SelectedValue);

            if (string.IsNullOrWhiteSpace(title))
            {
                ErrorSuccessNotifier.AddErrorMessage("Title cannot be empty");
                this.Response.Redirect("~/Admin/EditCategories.aspx");
            }

            if (string.IsNullOrWhiteSpace(author))
            {
                ErrorSuccessNotifier.AddErrorMessage("Author cannot be empty");
                this.Response.Redirect("~/Admin/EditCategories.aspx");
            }

            if (title.Length < 3 || title.Length > 256)
            {
                ErrorSuccessNotifier.AddErrorMessage("Title's length should be between 3 and 256 symbols");
                this.Response.Redirect("~/Admin/EditBooks.aspx");
            }

            if (author.Length < 3 || author.Length > 100)
            {
                ErrorSuccessNotifier.AddErrorMessage("Author's length should be between 3 and 100 symbols");
                this.Response.Redirect("~/Admin/EditBooks.aspx");
            }

            int bookId = int.Parse(e.CommandArgument.ToString());
            var book = context.Books.Include("Category").FirstOrDefault(b => b.Id == bookId);
            var category = context.Categories.FirstOrDefault(c => c.Id == categoryIndex);

            book.Title = title;
            book.Author = author;
            book.ISBN = isbn;
            book.WebSite = webSite;
            book.Description = description;
            book.Category = category;

            context.SaveChanges();

            ErrorSuccessNotifier.AddSuccessMessage("Book modified!");
            this.Response.Redirect("~/Admin/EditBooks.aspx");
        }
コード例 #23
0
        protected void SaveEditCategory_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                Button but = (Button)sender;
            int id = int.Parse(but.CommandArgument);
            string title = this.EditCategoryTitle.Text;

            var context = new LibrarySystemEntities();
            var category = context.Categories.Find(id);
            category.Name = title;

            try
            {
                context.SaveChanges();
                this.EditCategoryForm.Visible = false;
                this.EditCategoriesList.DataBind();
            }
            catch (Exception ex)
            {

                ErrorSuccessNotifier.AddErrorMessage(ex);
                ErrorSuccessNotifier.ShowAfterRedirect = true;
                Response.Redirect("../EditCategories.aspx");
            }
            }
            
        }
コード例 #24
0
        protected void DeleteBookButton_Command(object sender, CommandEventArgs e)
        {
            var context = new LibrarySystemEntities();

            int bookId = int.Parse(e.CommandArgument.ToString());
            var book = context.Books.FirstOrDefault(b => b.Id == bookId);

            context.Books.Remove(book);
            context.SaveChanges();

            ErrorSuccessNotifier.AddSuccessMessage("Book deleted!");
            this.Response.Redirect("~/Admin/EditBooks.aspx");
        }
コード例 #25
0
        protected void LinkButtonEditSave_Click(object sender, EventArgs e)
        {
            var categoryToEditId = Convert.ToInt32(this.LiteralCategoryToEditId.Text);
            var categoryToEditNewName = this.TextBoxEditCategoryName.Text;
            var context = new LibrarySystemEntities();
            var categoryToEdit = context.Categories.FirstOrDefault(c => c.CategoryId == categoryToEditId);
            if (categoryToEdit.Name == categoryToEditNewName)
            {
                ErrorSuccessNotifier.AddSuccessMessage("Category successfully edited!");
                return;
            }

            if (context.Categories.Any(c => c.Name == categoryToEditNewName))
            {
                ErrorSuccessNotifier.AddErrorMessage("Category with this name already exists!");
                return;
            }

            if (categoryToEdit == null)
            {
                ErrorSuccessNotifier.AddErrorMessage(
                    "An unexpected error occured! The category you want to edit was not found...");
            }
            else
            {
                categoryToEdit.Name = categoryToEditNewName;
                try
                {
                    context.SaveChanges();
                    this.GridViewCategories.DataBind();
                    ErrorSuccessNotifier.AddSuccessMessage("Category successfully edited!");
                }
                catch (Exception exc)
                {
                    ErrorSuccessNotifier.AddErrorMessage(exc);
                }
            }
        }