// The id parameter name should match the DataKeyNames value set on the control public void BooksListView_DeleteItem(int Id) { var db = new LibraryDbContext(); var selected = db.Books.Find(Id); db.Books.Remove(selected); db.SaveChanges(); }
public ActionResult BooksCreate([DataSourceRequest]DataSourceRequest request, BookViewModel book) { if (ModelState.IsValid) { var db = new LibraryDbContext(); Book newBook = new Book() { Description = book.Description, Author = book.Author, ISBN = book.ISBN, Title = book.Title, Website = book.Website }; var category = db.Categories.FirstOrDefault(x => x.Name == book.CategoryName); category = CreateOrGetCategory(book, db, newBook, category); newBook.Id = db.Books.Add(newBook).Id; db.SaveChanges(); book.Id = newBook.Id; } return Json(new[] { book }.ToDataSourceResult(request, ModelState)); }
public void BooksListView_InsertItem() { var item = new Book(); TryUpdateModel(item); if (ModelState.IsValid) { var db = new LibraryDbContext(); db.Books.Add(item); db.SaveChanges(); } }
public void CategoriesListView_InsertItem() { var item = new Category(); TryUpdateModel(item); if (ModelState.IsValid) { var db = new LibraryDbContext(); db.Categories.Add(item); db.SaveChanges(); } }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewCategories_DeleteItem(int id) { var db = new LibraryDbContext(); Category item = db.Categories.FirstOrDefault(c => c.Id == id); if (item != null) { db.Categories.Remove(item); db.SaveChanges(); } }
public ActionResult ActivateAccount2(MultipleModel.AuthModelVM request) { if (ModelState.IsValid) { using (var db = new LibraryDbContext()) { MultipleModel.AuthModelVM vm = new MultipleModel.AuthModelVM(); vm.UserModel = db.Users.SingleOrDefault(u => u.Id == request.UserModel.Id); var crypto = new SimpleCrypto.PBKDF2(); var encrypPass = crypto.Compute(request.ActivationModel1.Password); vm.UserModel.PasswordSalt = crypto.Salt; vm.UserModel.Password = encrypPass; vm.UserModel.SecretQuestion = request.ActivationModel1.SecretQuestion; vm.UserModel.SecretAnswer = request.ActivationModel1.SecretAnswer; vm.UserModel.Status = true; vm.UserModel.Deleted = false; vm.UserModel.UpdatedAt = DateTime.UtcNow; vm.UserModel.Student.Birthday = request.ActivationModel1.Birthday; vm.UserModel.Student.StudentAddress = new LibraryDbContext.StudentAddressModel { ZipCode = request.ActivationModel1.ZipCode, Address1 = request.ActivationModel1.Address1, Address2 = request.ActivationModel1.Address2, City = request.ActivationModel1.City, Country = request.ActivationModel1.Country, CreatedAt = DateTime.UtcNow, }; db.Entry(vm.UserModel).State = EntityState.Modified; db.SaveChanges(); var ctx = Request.GetOwinContext(); var authManager = ctx.Authentication; authManager.SignOut("ApplicationCookie"); var loginVM = new MultipleModel.LoginModelVM(); loginVM.Error = false; var errorList = new List <string>(); string message = "You have successfully activated your account. Please log in"; errorList.Add(message); loginVM.Message = errorList; TempData["LoginTD"] = loginVM; return(RedirectToAction("Login")); } } request.Error = true; request.Message = CustomValidationMessage.GetErrorList(ViewData.ModelState); TempData["UserActivation"] = request; return(RedirectToAction("ActivateAccount2", new { id = request.UserModel.Id })); }
public void CreateCategoryFormView_InsertItem() { LibraryDbContext db = new LibraryDbContext(); var item = new LibrarySystem.Models.Category(); TryUpdateModel(item); if (ModelState.IsValid) { db.Categories.Add(item); db.SaveChanges(); } this.CreateCategoryPanel.Visible = false; this.Response.Redirect(this.Request.RawUrl); }
//Create and Read Method private void BtnCustomerAdd_Click(object sender, EventArgs e) { DialogResult r = MessageBox.Show("Are you Sure?", "Yes", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (r == DialogResult.Yes) { if (!string.IsNullOrEmpty(TxtCustomerName.Text) || !string.IsNullOrEmpty(TxtCustomerSurname.Text) || !string.IsNullOrEmpty(TxtCustomerPhone.Text) || !string.IsNullOrEmpty(TxtCustomerEmail.Text) || !string.IsNullOrEmpty(TxtCustomerIdNumber.Text)) { Customer customer = new Customer//add user to database { Name = TxtCustomerName.Text, Surname = TxtCustomerSurname.Text, Phone = TxtCustomerPhone.Text, Email = TxtCustomerEmail.Text, IdentifyNumber = TxtCustomerIdNumber.Text, }; _context.Customers.Add(customer); _context.SaveChanges(); } else { MessageBox.Show("Please"); } DgvAddCustomer.Rows.Clear(); FillCustomers(); Clear(); } if (r == DialogResult.No) { Clear(); } }
public void RegisterUser(RegisterUserDto dto) { var newUser = new User() { Email = dto.Email, BirthDate = dto.BirthDate, RoleId = dto.RoleId }; var hashedPassword = _passwordHasher.HashPassword(newUser, dto.Password); newUser.PasswordHash = hashedPassword; _dbContext.Users.Add(newUser); _dbContext.SaveChanges(); }
public ActionResult Create(Book book) { if (!ModelState.IsValid) { return(RedirectToAction("Index")); } using (LibraryDbContext db = new LibraryDbContext()) { db.Books.Add(book); db.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult Delete(Book book) { using (LibraryDbContext db = new LibraryDbContext()) { if (book == null) { return(RedirectToAction("Index")); } db.Books.Remove(book); db.SaveChanges(); return(RedirectToAction("Index")); } }
private void CreateEditions() { var defaultEdition = _context.Editions.FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName); if (defaultEdition == null) { defaultEdition = new Edition { Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName }; _context.Editions.Add(defaultEdition); _context.SaveChanges(); //TODO: Add desired features to the standard edition, if wanted! } }
public int Register([FromBody] BookAuthor input) { var lst = libraryContext.Books.Where(x => x.Id == input.BookId).FirstOrDefault(); var lst1 = libraryContext.Authors.Where(x => x.Id == input.AuthorId).FirstOrDefault(); if (lst == null && lst1 == null) { return(0); } libraryContext.BookAuthors.Add(input); libraryContext.SaveChanges(); // mesle crtl s dar data base mibashad savechange return(input.Id); }
public int PostAuthor(AuthorDTO author) { var author1 = new Author { Id = author.Id, Name = author.Name, Surname = author.Surname, BookAuthors = null }; _context.Authors.Add(author1); _context.SaveChanges(); return(author1.Id); }
private void BtnCreateOrder_Click_1(object sender, EventArgs e) { if (_selectedCustomer == null || _selectedBook == null) { MessageBox.Show("Please select customer and book", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } DialogResult r = MessageBox.Show("Are you sure?", "Order confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (r == DialogResult.No) { return; } Order order = new Order { CustomerId = _selectedCustomer.Id, BookId = _selectedBook.Id, BookCount = Convert.ToInt32(NupBookCount.Value), TakenAt = DtpNewTaken.Value, Deadline = DtpNewDeadline.Value, Price = Convert.ToDecimal(RtbTotalPrice.Text) * NupBookCount.Value, IsOpen = true }; _context.Orders.Add(order); _selectedBook.Count -= Convert.ToInt32(NupBookCount.Value); _context.SaveChanges(); RtbTotalPrice.Clear(); NupBookCount.Value = 1; PnlOrderInfo.Visible = false; ClearOrders(); FillActiveOrders(); ClearBooks(); FillBooksSearch(); }
private void BtnCustomerDelete_Click(object sender, EventArgs e) { DialogResult d = MessageBox.Show("Əminsinizmi?", "Silmək", MessageBoxButtons.YesNo); if (d == DialogResult.Yes) { if (selected != null) { _context.Customers.Remove(selected); _context.SaveChanges(); } FillCustomerDgv(); } }
public void Get_Humanized_Branch_Hours() { var options = new DbContextOptionsBuilder <LibraryDbContext>() .UseInMemoryDatabase("Gets_branch_hours") .Options; using (var context = new LibraryDbContext(options)) { var branch = new LibraryBranch { Id = -190 }; var hours = new List <BranchHours> { new BranchHours { Branch = branch, DayOfWeek = 1, OpenTime = 13, CloseTime = 15 }, new BranchHours { Branch = branch, DayOfWeek = 2, OpenTime = 4, CloseTime = 24 } }; context.BranchHours.AddRange(hours); context.SaveChanges(); } using (var context = new LibraryDbContext(options)) { var sut = new LibraryBranchService(context); var result = sut.GetBranchHours(-190); var expected = new List <string> { "Monday 13:00 to 15:00", "Tuesday 04:00 to 00:00" }; result.Should().BeEquivalentTo(expected); } }
public IActionResult Edit(Book book) { if (ModelState.IsValid) { using (var db = new LibraryDbContext()) { Book currentBook = db.Books.FirstOrDefault(b => b.Id == book.Id); currentBook.Title = book.Title; currentBook.Author = book.Author; currentBook.Price = book.Price; db.SaveChanges(); } } return(RedirectToAction("Index")); }
public IActionResult Edit(Book book) { if (!ModelState.IsValid) { return(RedirectToAction("Index")); } using (var db = new LibraryDbContext()) { var bookToEdit = db.Books.FirstOrDefault(t => t.Id == book.Id); bookToEdit.Title = book.Title; bookToEdit.Author = book.Author; bookToEdit.Price = book.Price; db.SaveChanges(); } return(RedirectToAction("Index")); }
public IActionResult Delete(Book book) { using (var db = new LibraryDbContext()) { Book currentBook = db.Books.FirstOrDefault(b => b.Id == book.Id); if (currentBook == null) { return(RedirectToAction("Index")); } db.Books.Remove(currentBook); db.SaveChanges(); return(RedirectToAction("Index")); } }
private void SeedCategories(LibraryDbContext context) { var categories = new[] { new Domain.Entities.Category("Humour"), new Domain.Entities.Category("Drama"), new Domain.Entities.Category("Action"), new Domain.Entities.Category("Thriller"), new Domain.Entities.Category("Biographical"), new Domain.Entities.Category("Technical"), new Domain.Entities.Category("Comic") }; context.Categories.AddRange(categories); context.SaveChanges(); }
public void Delete(IssuedBook entity) { using (var db = new LibraryDbContext()) { var book = db.Books.FirstOrDefault(b => b.Id == entity.IdBook); if (book == null) { throw new SqliteException("book doesn't exists in database", 1); } book.Count++; db.IssuedBooks.Remove(entity); db.SaveChanges(); } }
// The id parameter name should match the DataKeyNames value set on the control public void BooksListView_UpdateItem(int Id) { var db = new LibraryDbContext(); Book item = db.Books.Find(Id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", Id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { db.SaveChanges(); } }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewCategories_UpdateItem(int categoryId) { LibraryDbContext db = new LibraryDbContext(); Category item = db.Categories.Find(categoryId); if (item == null) { ModelState.AddModelError("", String.Format("Item with id {0} was not found", categoryId)); return; } TryUpdateModel(item); if (ModelState.IsValid) { db.SaveChanges(); } }
// The id parameter name should match the DataKeyNames value set on the control public void ListViewCategories_UpdateItem(int Id) { Category item = dbContext.Categories.Find(Id); // Load the item here, e.g. item = MyDataLayer.Find(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", Id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { dbContext.SaveChanges(); } }
public ActionResult CategoriesUpdate([DataSourceRequest] DataSourceRequest request, CategoryViewModel category) { if (ModelState.IsValid) { var db = new LibraryDbContext(); var newCategory = new Category() { Id = category.Id, Name = category.Name }; db.Categories.Attach(newCategory); db.Entry(newCategory).State = EntityState.Modified; db.SaveChanges(); } return(Json(new[] { category }.ToDataSourceResult(request, ModelState))); }
public void Get_Current_Patron() { var options = new DbContextOptionsBuilder <LibraryDbContext>() .UseInMemoryDatabase("Gets_current_patron") .Options; using (var context = new LibraryDbContext(options)) { var card = new LibraryCard { Id = 3233 }; var patron = new Patron { FirstName = "Kevin", LastName = "Shields", LibraryCard = card }; context.Patrons.Add(patron); var checkout = new Checkout { Id = 1999, LibraryAsset = new Video { Id = 9, Title = "Stranger Things 2" }, LibraryCard = card }; context.Checkouts.Add(checkout); context.SaveChanges(); } using (var context = new LibraryDbContext(options)) { var sut = new CheckoutService(context); var result = sut.GetCurrentPatron(9); result.Should().Be("Kevin Shields"); } }
public IActionResult Delete(Book book) { using (var db = new LibraryDbContext()) { var bookToRemove = db.Books.FirstOrDefault(b => b.Id == book.Id); if (bookToRemove != null) { db.Books.Remove(bookToRemove); db.SaveChanges(); } else { // Log error message } } return(RedirectToAction("Index")); }
public ActionResult CategoriesDelete([DataSourceRequest] DataSourceRequest request, CategoryViewModel category) { if (ModelState.IsValid) { var db = new LibraryDbContext(); var toDelete = new Category() { Id = category.Id, Name = category.Name }; db.Categories.Attach(toDelete); db.Categories.Remove(toDelete); db.SaveChanges(); } return(Json(new[] { category }.ToDataSourceResult(request, ModelState))); }
public IActionResult Create(Book book) { if (ModelState.IsValid) { using (var db = new LibraryDbContext()) { db.Books.Add(book); db.SaveChanges(); return(RedirectToAction("Index")); } } else { // Log error return(RedirectToAction("Index")); } }
public ActionResult CategoriesCreate([DataSourceRequest]DataSourceRequest request, CategoryViewModel category) { if (ModelState.IsValid) { var db = new LibraryDbContext(); Category newCategory = new Category() { Name = category.Name }; newCategory.Id = db.Categories.Add(newCategory).Id; db.SaveChanges(); category.Id = newCategory.Id; } return Json(new[] { category }.ToDataSourceResult(request, ModelState)); }
public ActionResult CategoriesUpdate([DataSourceRequest]DataSourceRequest request, CategoryViewModel category) { if (ModelState.IsValid) { var db = new LibraryDbContext(); var newCategory = new Category() { Id = category.Id, Name = category.Name }; db.Categories.Attach(newCategory); db.Entry(newCategory).State = EntityState.Modified; db.SaveChanges(); } return Json(new[] { category }.ToDataSourceResult(request, ModelState)); }
public ActionResult CategoriesCreate([DataSourceRequest] DataSourceRequest request, CategoryViewModel category) { if (ModelState.IsValid) { var db = new LibraryDbContext(); Category newCategory = new Category() { Name = category.Name }; newCategory.Id = db.Categories.Add(newCategory).Id; db.SaveChanges(); category.Id = newCategory.Id; } return(Json(new[] { category }.ToDataSourceResult(request, ModelState))); }
public static void FillBooksDb(this LibraryDbContext context) { context.Books.Add( new Book { Id = "0", Isbn = "Isbn 0", Title = "Title 0", Year = "2019" } ); context.Books.Add( new Book { Id = "1", Isbn = "Isbn 1", Title = "Title 1", Year = "2018" } ); context.Books.Add( new Book { Id = "2", Isbn = "Isbn 2", Title = "Title 2", Year = "2019" } ); context.Books.Add( new Book { Id = "3", Isbn = "Isbn 3", Title = "Title 3", Year = "2017" } ); context.SaveChanges(); }
public IActionResult Create(string title, string author, double price) { if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(author) || price < 1) { return(RedirectToAction("Index")); } using (var db = new LibraryDbContext()) { Book book = new Book { Title = title, Author = author, Price = price }; db.Books.Add(book); db.SaveChanges(); } return(RedirectToAction("Index")); }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewCategories_UpdateItem(int id) { var db = new LibraryDbContext(); Category item = db.Categories.FirstOrDefault(c => c.Id == id); // Load the item here, e.g. item = MyDataLayer.Find(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here, e.g. MyDataLayer.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category modified"); db.SaveChanges(); } }
public IActionResult Delete(Book book) { if (book != null) { using (LibraryDbContext db = new LibraryDbContext()) { Book bookToDelete = db.Books.FirstOrDefault(x => x.Id == book.Id); if (bookToDelete == null) { return(RedirectToAction("Index")); } db.Books.Remove(bookToDelete); db.SaveChanges(); } } return(RedirectToAction("Index")); }
private void BtnSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxBookName.Name)) { MessageBox.Show("Insert Book Name"); return; } BookInfo book = new BookInfo(); book.BookName = textBoxBookName.Text; book.BookAuthor = textBoxAuthor.Text; book.BookPublicationName = textBoxPublication.Text; book.BookPurchaseDate = DateTime.Parse(dateTimePickerBook.Text); book.BookPrice = textBoxPrice.Text; book.BookQuantity = int.Parse(textBoxQuantity.Text); _context.Books.Add(book); _context.SaveChanges(); }
public IActionResult Create(string title, string author, double price, Book book) { if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(author) || price == 0) { return(RedirectToAction("Index")); } book.Title = title; book.Author = author; book.Price = price; using (var db = new LibraryDbContext()) { db.Books.Add(book); db.SaveChanges(); } return(RedirectToAction("Index")); }
protected void ButtonCreateBook_Click(object sender, EventArgs e) { string title = this.TextBoxBookTitle.Text; string author = this.TextBoxBookAuthor.Text; string isbn = this.TextBoxBookISBN.Text; string webSite = this.TextBoxBookWebSite.Text; string description = this.TextBoxBookDescription.Text; string categoryId = this.DropDownListCategories.SelectedValue; if (string.IsNullOrEmpty(title)) { ErrorSuccessNotifier.AddErrorMessage("Book title cannot be empty."); return; } if (string.IsNullOrEmpty(author)) { ErrorSuccessNotifier.AddErrorMessage("Book author cannot be empty."); return; } if (string.IsNullOrEmpty(categoryId)) { ErrorSuccessNotifier.AddErrorMessage("Book category cannot be empty."); return; } var db = new LibraryDbContext(); int catId = int.Parse(categoryId); Category bookCategory = db.Categories.FirstOrDefault(b => b.Id == catId); Book book = new Book() { Title = title, Author = author, ISBN = isbn, WebSite = webSite, Description = description, Category = bookCategory }; try { db.Books.Add(book); db.SaveChanges(); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } this.TextBoxBookTitle.Text = ""; this.TextBoxBookAuthor.Text = ""; this.TextBoxBookISBN.Text = ""; this.TextBoxBookWebSite.Text = ""; this.TextBoxBookDescription.Text = ""; ErrorSuccessNotifier.AddSuccessMessage("Book created"); this.ButtonCreateNewBookPanel.Visible = true; this.PanelCreateNewBook.Visible = false; this.GridViewBooks.DataBind(); }
protected void ButtonUpdateCategory_Click(object sender, EventArgs e) { string newName = this.TextBoxUpdateCategory.Text; int categoryId = int.Parse(this.CurrentCategory.Text); var db = new LibraryDbContext(); var item = db.Categories.FirstOrDefault(c => c.Id == categoryId); item.Name = newName; db.SaveChanges(); this.GridViewCategories.DataBind(); ErrorSuccessNotifier.AddSuccessMessage("Category modified."); this.PanelUpdateCategory.Visible = false; }
protected void ButtonYesDelete_Click(object sender, EventArgs e) { int categoryId = int.Parse(this.CurrentCategory.Text); var db = new LibraryDbContext(); var item = db.Categories.Include("Books").FirstOrDefault(c => c.Id == categoryId); if (item.Books != null) { db.Books.RemoveRange(item.Books); } db.Categories.Remove(item); db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category deleted."); this.PanelDelete.Visible = false; this.GridViewCategories.DataBind(); }
protected void ButtonCreateCategory_Click(object sender, EventArgs e) { string categoryName = this.TextBoxCategoryName.Text; var category = new Category(); if (!string.IsNullOrEmpty(categoryName)) { category.Name = categoryName; var db = new LibraryDbContext(); db.Categories.Add(category); db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category created"); } else { ErrorSuccessNotifier.AddErrorMessage("Category name cannot be empty."); } this.ButtonShowCreateNewForm.Visible = true; this.PanelCreateNewCategory.Visible = false; }
public ActionResult BooksDelete([DataSourceRequest]DataSourceRequest request, Book book) { if (ModelState.IsValid) { var db = new LibraryDbContext(); db.Books.Attach(book); db.Books.Remove(book); db.SaveChanges(); } return Json(new[] { book }.ToDataSourceResult(request, ModelState)); }
public ActionResult BooksUpdate([DataSourceRequest]DataSourceRequest request, BookViewModel book) { if (ModelState.IsValid) { var db = new LibraryDbContext(); var category = db.Categories.FirstOrDefault(x => x.Name == book.CategoryName); var oldBook = db.Books.Single(x => x.Id == book.Id); if (oldBook.Category.Name != book.CategoryName) { oldBook.Category = category; UpdateBookFields(book, oldBook); category.Books.Add(oldBook); db.SaveChanges(); } else { UpdateBookFields(book, oldBook); db.Entry(oldBook).State = EntityState.Modified; db.SaveChanges(); } } return Json(new[] { book }.ToDataSourceResult(request, ModelState)); }
public ActionResult CategoriesDelete([DataSourceRequest]DataSourceRequest request, CategoryViewModel category) { if (ModelState.IsValid) { var db = new LibraryDbContext(); var toDelete = new Category() { Id = category.Id, Name = category.Name }; db.Categories.Attach(toDelete); db.Categories.Remove(toDelete); db.SaveChanges(); } return Json(new[] { category }.ToDataSourceResult(request, ModelState)); }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewBooks_DeleteItem(int id) { var db = new LibraryDbContext(); Book item = db.Books.FirstOrDefault(b => b.Id == id); db.Books.Remove(item); db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Book deleted"); }
protected void ButtonEditSave_Click(object sender, EventArgs e) { int bookId = int.Parse(this.CurrentBook.Text); var db = new LibraryDbContext(); var book = db.Books.FirstOrDefault(b => b.Id == bookId); if (string.IsNullOrEmpty(this.DropDownListEditBookCategory.SelectedValue)) { ErrorSuccessNotifier.AddErrorMessage("Please select category from drop down menu."); return; } int newCategoryId = int.Parse(this.DropDownListEditBookCategory.SelectedValue); var cat = db.Categories.FirstOrDefault(c => c.Id == newCategoryId); book.Title = this.TextBoxEditBookTitle.Text; book.Author = this.TextBoxEditBookAuthor.Text; book.ISBN = this.TextBoxEditBookISBN.Text; book.WebSite = this.TextBoxEditBookSite.Text; book.Description = this.TextBoxEditBookDescription.Text; book.Category = cat; db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Book Updated"); this.PanelEditBook.Visible = false; this.GridViewBooks.DataBind(); }
protected void ButtonDeleteBookYes_Click(object sender, EventArgs e) { int bookId = int.Parse(this.CurrentBook.Text); var db = new LibraryDbContext(); var book = db.Books.FirstOrDefault(b => b.Id == bookId); if (book != null) { db.Books.Remove(book); db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Book deleted"); this.GridViewBooks.DataBind(); this.PanelDeleteBook.Visible = false; } else { ErrorSuccessNotifier.AddErrorMessage("Book is missing."); } }