private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) //event handle when picking an item { try { MySqlConnection connection = new MySqlConnection("Datasource=localhost;port=3306;username=root;password="******"\"{comboBox1.SelectedItem.ToString()}\""; string query = "SELECT * FROM bookstore.books WHERE title = " + selectedItem; connection.Open(); MySqlCommand command = new MySqlCommand(query, connection); MySqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { AuthorText.Text = reader["author"].ToString(); PriceText.Text = reader["price"].ToString(); IsbnText.Text = reader["ISBN"].ToString(); } connection.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); //clear form AuthorText.Clear(); IsbnText.Clear(); PriceText.Clear(); } QuantityText.Focus(); }
public void AddTitle_Click(object sender, EventArgs e) { //get title of book try { int Quantity; bool Qty = int.TryParse(QuantityText.Text, out Quantity); //grab number input if (Qty && Quantity != 0 && !(Quantity < 0)) //handle exception 0 and negative numbers { decimal totalCost = Quantity * Convert.ToDecimal(PriceText.Text); //Get the total // Populate the rows. string selectedItem = (string)comboBox1.SelectedItem; string[] row = new string[] { selectedItem, PriceText.Text, Quantity.ToString(), totalCost.ToString() }; //populate and add row //populate dataGridView upon click Add Title dataGridView1.Rows.Add(row); subTotal += Quantity * Convert.ToDouble(PriceText.Text); //add total Subtotal_Text.Text = Math.Round(subTotal, 2).ToString(); TaxText.Text = (Math.Round(subTotal * tax, 2)).ToString(); TotalText.Text = (Math.Round((subTotal * tax) + subTotal, 2)).ToString(); } else { MessageBox.Show("Please enter a valid number"); QuantityText.Focus();//cursor on field } } catch { MessageBox.Show("Please select a book from the list."); } }
/// <summary> /// When the "Add Title" button is clicked, the selected book and all of its characteristics will appear in /// dataGridView1 and also update the subtotal, tax, and total boxes with correct values and information /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddTitle_Click(object sender, EventArgs e) { //get title try { int quantity; bool QTY = int.TryParse(QuantityText.Text, out quantity); if (QTY && quantity != 0 && !(quantity < 0)) { decimal? totalCost = quantity * Convert.ToDecimal(PriceText.Text); string SelectedItem = (string)comboBox1.SelectedItem; string[] row = new string[] { SelectedItem, "$" + PriceText.Text, quantity.ToString(), "$" + totalCost.ToString() }; dataGridView1.Rows.Add(row); subTotal += quantity * Convert.ToDouble(PriceText.Text); Subtotal_Text.Text = "$" + subTotal.ToString(); TaxText.Text = (subTotal * tax).ToString(); TotalText.Text = "$" + ((subTotal * tax) + subTotal).ToString(); QuantityText.Text = quantity.ToString(); } else { MessageBox.Show("Enter a Number"); QuantityText.Focus(); } } catch { MessageBox.Show("Select a Book"); } }
/// <summary> /// This creates the book and if a certain book is picked, the corresponding text boxes will update with the /// correct information in each box. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string SelectedItem = (string)comboBox1.SelectedItem; try { string jsonBook = File.ReadAllText(@"Z:\Desktop\Fourth Year (F18-S19)\CompE561\Lab1\Lab1\bin\Debug\BookList.json"); JObject JSON = JObject.Parse(jsonBook); JObject targetBook = (JObject)JSON[SelectedItem]; string s_targetBook = targetBook.ToString(); Book found = new Book(); Newtonsoft.Json.JsonConvert.PopulateObject(s_targetBook, found); AuthorText.Text = found.author;; IsbnText.Text = found.ISBN; PriceText.Text = found.price.ToString(); } catch { AuthorText.Clear(); IsbnText.Clear(); PriceText.Clear(); } QuantityText.Focus(); }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) //event handle when picking an item { string SelectedItem = (string)comboBox1.SelectedItem; try { //access books string BookJSON = File.ReadAllText(@"C:\Users\RMBonMAC\Documents\GitHub\BookStore\BookStore\BookStore\bin\Debug\Books.json"); JObject json = JObject.Parse(BookJSON); JObject BookTarget = (JObject)json[SelectedItem]; string book_target = BookTarget.ToString(); Book foundBook = new Book(); Newtonsoft.Json.JsonConvert.PopulateObject(book_target, foundBook); AuthorText.Text = foundBook.author;; IsbnText.Text = foundBook.ISBN; PriceText.Text = foundBook.price.ToString(); } catch { AuthorText.Clear(); IsbnText.Clear(); PriceText.Clear(); } QuantityText.Focus(); }