/// <summary> /// Event handler for key presses in ISBNtextBox /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ISBNTextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) //if enter key is pressed, attempt to add the book to the BookListView { AddBookToList(); ISBNTextBox.Text = string.Empty; ISBNTextBox.Focus(); } }
/// <summary> /// Verifies the user's ISBN entry which must be a 10 or 13 digit code. /// If ISBN is valid, matches a Book in the collection, and has copies available for checkout, /// add the Book to be shown in BooksListView. /// </summary> private void AddBookToList() { //Edit the user input to remove non-digit characters (spaces, etc.) StringBuilder sb = new StringBuilder(); foreach (char c in ISBNTextBox.Text) { if (Char.IsDigit(c)) //check if character is a digit { sb.Append(c); } else if (Char.IsLetter(c)) //if a letter is in the code, not valid. { ErrorLabel.Content = "ISBN is not valid, must be a 10 or 13 digit code."; return; } } string token = sb.ToString(); //get the resulting isbn if (token.Length != 10 && token.Length != 13) //if isbn length is incorrect, return { ErrorLabel.Content = "ISBN is not valid, must be a 10 or 13 digit code"; return; } Book b = controller.SearchISBN(token); //isbn is valid, retrieve book from collection if (b == null) //book not found { ErrorLabel.Content = "Book not found. Please verify the ISBN."; return; } else //book found { if (b.CopiesAvailable == 0) //no available copies { ErrorLabel.Content = "No copy of this book available for check out."; return; } else //Add book to ObservableCollection and set focus to ISBNTextBox { books.Add(b); ISBNTextBox.Focus(); CheckOutButton.IsEnabled = true; } } }
// save button clicked private void SaveButton_Click(object sender, EventArgs e) { if (TitleTextBox.ForeColor != Color.Red && AuthorTextBox.ForeColor != Color.Red && ISBNTextBox.ForeColor != Color.Red && PriceTextBox.ForeColor != Color.Red) { // adding new book if (addNewBookMode) { DialogResult save = MessageBox.Show("Do you want to save the book?", "Add New Book", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (save == DialogResult.Yes) { scmd = new SqlCommand("select count(*) from Books where (ISBN = @isbn)", connection); scmd.Parameters.AddWithValue("@isbn", ISBNTextBox.Text); connection.Open(); int exists = (int)scmd.ExecuteScalar(); // check if entered isnb is unique connection.Close(); if (exists == 0) { scmd = new SqlCommand("insert into Books(Title, Author, ISBN, Price) Values(@title, @author, @isbn, @price)", connection); connection.Open(); scmd.Parameters.AddWithValue("@title", TitleTextBox.Text); scmd.Parameters.AddWithValue("@author", AuthorTextBox.Text); scmd.Parameters.AddWithValue("@isbn", ISBNTextBox.Text); scmd.Parameters.AddWithValue("@price", PriceTextBox.Text); scmd.ExecuteNonQuery(); connection.Close(); MessageBox.Show("The Book Was Added Successfully."); DisplayBooks(); // refresh combobox values ResetAll(); BooksComboBox.Enabled = true; } else { MessageBox.Show("Provided ISBN code already exists in the database.", "Invalid ISBN Code", MessageBoxButtons.OK, MessageBoxIcon.Error); ISBNTextBox.Focus(); } } } // updating existing book if (updateBookMode) { DialogResult update = MessageBox.Show("Do you want to update the book?", "Update Existing Book", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (update == DialogResult.Yes) { string cmdText = "UPDATE Books SET Title = @title, Author = @author, Price = @price WHERE ISBN = @isbn"; scmd = new SqlCommand(cmdText, connection); connection.Open(); scmd.Parameters.AddWithValue("@title", TitleTextBox.Text); scmd.Parameters.AddWithValue("@author", AuthorTextBox.Text); scmd.Parameters.AddWithValue("@isbn", ISBNTextBox.Text); scmd.Parameters.AddWithValue("@price", PriceTextBox.Text); scmd.ExecuteNonQuery(); connection.Close(); MessageBox.Show("The Book Was Updated Successfully."); DisplayBooks(); // refresh combobox values ResetAll(); } } } else { MessageBox.Show("Make sure you have provided valid inputs for all the fields", "Invalid Input Provided", MessageBoxButtons.OK, MessageBoxIcon.Error); if (TitleTextBox.ForeColor == Color.Red) { TitleTextBox.Focus(); } if (AuthorTextBox.ForeColor == Color.Red) { AuthorTextBox.Focus(); } if (ISBNTextBox.ForeColor == Color.Red) { ISBNTextBox.Focus(); } if (PriceTextBox.ForeColor == Color.Red) { PriceTextBox.Focus(); } } }