예제 #1
0
 private void DeleteBook(int bookId)
 {
     using (var context = new LibraryEntities())
     {
         var book = context.Books.FirstOrDefault(b => b.Id == bookId);
         context.Books.Remove(book);
         context.SaveChanges();
     }
 }
예제 #2
0
 private bool InventoryNumberUsed(string inventoryNumber)
 {
     using (var content = new LibraryEntities())
     {
         var book = content.Books.Where(x => x.InventoryNumber.Equals(inventoryNumber)).ToList();
         if (book.Count > 0)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
예제 #3
0
 private void InitializeBookDetails(int bookId)
 {
     using (var context = new LibraryEntities())
     {
         var book = context.Books.FirstOrDefault(b => b.Id == bookId);
         if (book != null)
         {
             textBoxInventoryNumber.Text     = book.InventoryNumber;
             textBoxInventoryNumber.ReadOnly = true;
             textBoxTitle.Text       = book.Title;
             textBoxAuthor.Text      = book.Author;
             textBoxDescription.Text = book.Description;
         }
     }
 }
예제 #4
0
        private void LoadBooks()
        {
            using (var context = new LibraryEntities())
            {
                var books = context.Books.Select(x => new
                {
                    Id = x.Id,
                    InventoryNumber = x.InventoryNumber,
                    Title           = x.Title,
                    Author          = x.Author,
                    Description     = x.Description
                }).ToList();

                this.booksGridView.DataSource = books;
            }
        }
예제 #5
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (IsValid())
     {
         using (var connection = new LibraryEntities())
         {
             try
             {
                 if (!isEditForm)
                 {
                     var book = new Books
                     {
                         InventoryNumber = textBoxInventoryNumber.Text,
                         Title           = textBoxTitle.Text,
                         Author          = textBoxAuthor.Text,
                         Description     = textBoxDescription.Text
                     };
                     connection.Books.Add(book);
                     connection.SaveChanges();
                 }
                 else
                 {
                     var book = connection.Books.FirstOrDefault(b => b.Id == _bookId);
                     if (book == null)
                     {
                         MessageBox.Show("An error occured while saving the book");
                     }
                     else
                     {
                         book.InventoryNumber = textBoxInventoryNumber.Text;
                         book.Author          = textBoxAuthor.Text;
                         book.Title           = textBoxTitle.Text;
                         book.Description     = textBoxDescription.Text;
                         connection.SaveChanges();
                     }
                 }
                 this.Dispose();
                 MessageBox.Show("Book was saved successfully");
             }
             catch (Exception exception)
             {
                 this.Dispose();
                 MessageBox.Show("An error occured while saving the book");
             }
         }
     }
 }