// Adds product by name to the list of products and appends it to DB
 private void btnAddProducts_Click(object sender, EventArgs e)
 {
     if (Validator.IsProvided(txtProducts, "Product Name"))
     {
         string product = txtProducts.Text;
         bool   valid   = true;
         foreach (Product p in products)
         {
             if (p.ProdName == product)
             {
                 MessageBox.Show(product + " already exists");
                 valid = false;
                 break;
             }
         }
         if (valid)
         {
             Product pro = new Product();
             pro.ProdName  = product;
             pro.ProductId = ProductDB.AddProduct(pro);
             products      = ProductDB.GetProducts();
             displayProducts();
             listProducts();
             txtProducts.Clear();
             MessageBox.Show(product + " was added");
         }
     }
 }
Exemple #2
0
 private void btnCreate_Click(object sender, EventArgs e)
 {
     if (txtCreate.Text != null)
     {
         ProductDB.AddProduct(txtCreate.Text);
         txtCreate.Text = "";
     }
     product = ProductDB.GetAllProducts();
     dataGridView1.DataSource = product;
     this.Refresh();
 }
        private void BtnAccept_Click(object sender, EventArgs e)
        {
            if (IsValidData())
            {
                if (addproducts) // processing Add
                {
                    products = new Product();
                    PutProduct(products);

                    try
                    {
                        ProductDB.AddProduct(products);
                        MessageBox.Show("New Item with Product Id of " + products.ProductID + " and Product Name of " + products.ProdName + " was added");
                        this.DialogResult = DialogResult.OK;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, ex.GetType().ToString());
                    }
                }
                else // processing Modify
                {
                    Product newProduct = new Product();
                    newProduct.ProductID = Convert.ToInt32(txtProductID.Text);
                    txtProductID.Enabled = false;
                    newProduct.ProdName  = txtProdName.Text;
                    PutProduct(newProduct);
                    try
                    {
                        if (!ProductDB.UpdateProduct(newProduct, modifyProduct))
                        {
                            MessageBox.Show("Another user has updated or " +
                                            "deleted that product.", "Database Error");
                            this.DialogResult = DialogResult.Retry;
                        }
                        else
                        {
                            products          = newProduct;
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, ex.GetType().ToString());
                    }
                }
            }
        }