// ------------Added by Wei Guang Yan---------------- // Method of inserting product name to database public void AddProduct(string txtAdd) { // Innitialize for messagebox string message; string title = "New Product Insert"; MessageBoxButtons button = MessageBoxButtons.OK; MessageBoxIcon icon; // Add new item to database bool flag = ProductsDB.AddProduct(txtAdd); // Check whether adding excution is successful, and show message if (flag == true) { comboBox2.DataSource = ProductsDB.GetProducts(); comboBox2.DisplayMember = "ProdName"; comboBox2.ValueMember = "ProductId"; comboBox2.SelectedIndex = comboBox2.Items.Count - 1; txtAddItem.Text = ""; message = "The product is added successfully!"; icon = MessageBoxIcon.None; } else { message = "Error: Fail to add new product!\n Please try again or contact IT supports."; icon = MessageBoxIcon.Error; } MessageBox.Show(message, title, button, icon); }
private void btnAccept_Click(object sender, EventArgs e) { product = new Product(); if (string.IsNullOrEmpty(txbProductName.Text)) { MessageBox.Show("Please use numeric characters for 'Product ID'", "Input Format Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } product.ProductId = 0; product.ProdName = txbProductName.Text; if (xNewProduct) { try { ProductsDB.AddProduct(product); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else { Product newProduct = new Product(); newProduct.ProductId = Convert.ToInt32(txbProductID.Text); newProduct.ProdName = txbProductName.Text; product.ProductId = newProduct.ProductId; // The same ID try { if (!ProductsDB.UpdateProduct(product, newProduct)) { MessageBox.Show("Another user has updated or " + "deleted that product.", "Database Error"); this.DialogResult = DialogResult.Retry; } else { product = newProduct; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
private void btnAddProduct_Click(object sender, EventArgs e) { product = new Products(); this.PutProductData(product); try { product.ProductId = ProductsDB.AddProduct(product); MessageBox.Show("Product added", "Success!"); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show("Another user has updated or " + "deleted that product.", "Database Error"); this.DialogResult = DialogResult.Retry; } this.Close(); }
private void btnAccept_Click(object sender, EventArgs e) { if (addProduct) { product = new Products(); this.PutProductData(product); try { product.ProductId = ProductsDB.AddProduct(product); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else { Products newProduct = new Products(); newProduct.ProductId = product.ProductId; this.PutProductData(newProduct); try { if (!ProductsDB.UpdateProduct(product, newProduct)) { MessageBox.Show("Another user has updated or " + "deleted that product.", "Database Error"); this.DialogResult = DialogResult.Retry; } else { product = newProduct; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }