private bool UpdateProductEntry() { string title = titleBox.Text; if (String.IsNullOrWhiteSpace(title)) { MessageBox.Show("Title cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } string category = catBox.Text; if (String.IsNullOrWhiteSpace(category)) { MessageBox.Show("Category cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (!double.TryParse(priceBox.Text, out double price)) { MessageBox.Show("Price must be a number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } product.Title = titleBox.Text; product.Price = price; product.Category = category; ProductDb.Update(product); return(true); }
private void populateProductComboBox() { List <Product> allProducts = ProductDb.GetAllProducts(); allProductsCbo.DataSource = allProducts; allProductsCbo.DisplayMember = nameof(Product.Title); }
private void deleteProductBtn_Click(object sender, EventArgs e) { Product currProduct = allProductsCbo.SelectedItem as Product; DialogResult result = MessageBox.Show($"Are you sure you want to delete {currProduct.Title} for ${currProduct.Price} from the database?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { ProductDb.Delete(currProduct); populateProductComboBox(); } }
private void addProdBtn_Click(object sender, EventArgs e) { // Checks if title and category are null or whitespace string title = titleBox.Text; if (String.IsNullOrWhiteSpace(title)) { MessageBox.Show("Title cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string category = categoryBox.Text; if (String.IsNullOrWhiteSpace(category)) { MessageBox.Show("Category cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Attempts to parse double from priceBox, if it fails it shows a mbox and returns, if it succeeds the // variable price will contain the price if (!double.TryParse(priceBox.Text, out double price)) { MessageBox.Show("Price must be a number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Instantiates a product with the data collected from the form Product product = new Product { Title = title, Price = price, Category = category }; // Adds product object to database ProductDb.Add(product); // Displays success message and clears textboxs MessageBox.Show("Product was added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); titleBox.Text = ""; priceBox.Text = ""; categoryBox.Text = ""; }