Esempio n. 1
0
        /// <summary>
        /// Adds a single book to the database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddBookBtn_Click(object sender, EventArgs e)
        {
            //If user enters valid data
            if (isDataValid() == true)
            {
                MessageBox.Show("Saved the book to the database");

                //Creates a single book object
                Book b = new Book()
                {
                    ISBN  = isbnTxt.Text,
                    Title = titleTxt.Text,
                    Price = Convert.ToDecimal(priceTxt.Text)
                };

                //Once a book object has been created,
                //set the new book into that property (Book.cs)
                NewBook = b;

                try
                {
                    //Add the book to the database
                    BookDB.Add(b); //This .add is from BookDB's add method
                    DialogResult = DialogResult.OK;
                }
                catch
                {
                    MessageBox.Show("We're having server issues");
                }
            }
        }
        private void PopulateBookList()
        {
            //Populate the allBooks list of books from the database
            List <Book> allBooks = BookDB.GetAllBooks();

            //Start with an empty list, so it doesn't re-add previous books causing duplicates
            bookComboBox.Items.Clear();

            //Adds all of the books from the database in the bookComboBox
            foreach (Book b in allBooks)
            {
                bookComboBox.Items.Add(b);
            }
        }
Esempio n. 3
0
 private void PopulateBookList()
 {
     cboBook.Items.Clear();
     try
     {
         List <Book> books = BookDB.GetAllBooks();
         foreach (Book b in books)
         {
             cboBook.Items.Add(b);
         }
     }
     catch (SqlException sqlex)
     {
         MessageBox.Show("We are having trouble loading book data at this time, please try again later.");
         Application.Exit();
     }
 }
        private void DeleteBookBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(bookComboBox.Text))
            {
                MessageBox.Show("Please select a book");
                return;
            }

            //Get the selected book
            Book selectedBook = (Book)bookComboBox.SelectedItem;

            string message = $"Are you sure you want to delete {selectedBook.Title} {selectedBook.ISBN}?";

            DialogResult result = MessageBox.Show(text: message,
                                                  caption: "Delete?",
                                                  buttons: MessageBoxButtons.YesNo,
                                                  icon: MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                try
                {
                    //Remove in database
                    BookDB.Delete(selectedBook.ISBN);

                    //Remove from the list
                    customerComboBox.Items.Remove(selectedBook);

                    MessageBox.Show("Book deleted");

                    PopulateBookList();

                    bookComboBox.Text = "";
                }
                catch (SqlException)
                {
                    MessageBox.Show("Delete unsuccessful. The book you are trying to delete is currently registered to a customer.");
                }
                catch (Exception)
                {
                    MessageBox.Show("No books deleted");
                }
            }
        }
Esempio n. 5
0
        private void btnAddBook_Click(object sender, EventArgs e)
        {
            Book addBooks = new Book()
            {
                ISBN  = txtISBN.Text,
                Price = txtPrice.Text,
                Title = txtTitle.Text
            };

            try
            {
                if (bookToAdd == null)
                {
                    BookDB.AddBooks(addBooks);
                    MessageBox.Show("Book added");
                }
            }
            catch (SqlException sqlex)
            {
                MessageBox.Show("We are having server issues, try again later.");
            }
            this.Close();
        }