Esempio n. 1
0
        /// <summary>
        /// Add selected product to package in detail view
        /// </summary>
        /// @author Harry
        private void btnAddProdToPkg_Click(object sender, EventArgs e)
        {
            if (cmboBoxSupsOfProd.SelectedIndex > -1) // selection required
            {
                // get product and supplier ids (to get ProductSupplierId)
                int prodId     = products[cmboBoxProducts.SelectedIndex].ProductID;
                int supplierId = suppliersOfProd[cmboBoxSupsOfProd.SelectedIndex].SupplierId;

                try
                {
                    // get product supplier id
                    int prodSuppId = ProductSupplierDB.GetId(prodId, supplierId);

                    // create new package product supplier object to enter into package products suppliers table
                    PackageProductSupplier newPkgProdSup = new PackageProductSupplier();
                    newPkgProdSup.PackageId         = Convert.ToInt32(packageIdTextBox.Text);
                    newPkgProdSup.ProductSupplierId = prodSuppId;

                    // update packages product supplier table
                    bool success = PackageProductSupplierDB.AddNewPkgProdSupp(newPkgProdSup);

                    if (success)
                    {
                        // clear combo boxes
                        cmboBoxProducts.Items.Clear();
                        cmboBoxSupsOfProd.Items.Clear();

                        // show updated version of products associated with package
                        List <string> pkgProducts;
                        List <string> pkgProdSupps;

                        // access products and suppliers associated with current package
                        PackageDB.GetPackageProducts(packages[pkgPos].PackageId, out pkgProducts, out pkgProdSupps);
                        ShowPkgProducts(pkgProducts, pkgProdSupps);
                        ShowAllProducts();
                        cmboBoxSupsOfProd.Text = "";
                        cmboBoxProducts.Text   = "";
                        cmboBoxSupsOfProd.Items.Clear();
                        MessageBox.Show("Added product successfully.", "Success");
                    }
                    else
                    {
                        // clear combo boxes
                        cmboBoxProducts.Items.Clear();
                        cmboBoxSupsOfProd.Items.Clear();
                        MessageBox.Show("Error occured while adding product.", "Failure");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error while adding product to package\n" + ex.Message,
                                    ex.GetType().ToString());
                }
            }
            else
            {
                MessageBox.Show("Please select a product and its supplier.", "Select Error");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Operations for click event on Add Supplier button.
        /// </summary>
        /// @author Chi
        private void btnAddSupplier_Click(object sender, EventArgs e)
        {
            Supplier curSupplier;                                              // Currently select supplier
            int      currentSupID;                                             // Currently select supplier's ID
            int      currentProductID     = Convert.ToInt32(txtPSProdID.Text); // Current product's ProductID
            int      currentSupplierIndex = lstboxSuppliers.SelectedIndex;     // Current index of supplier selected in list box

            if (currentSupplierIndex >= 0)                                     // If user have selected an item from list box
            {
                // Set current supplier's ID to supplier selected in list box.
                curSupplier  = filteredSuppliers[currentSupplierIndex];
                currentSupID = curSupplier.SupplierId;

                // Verify if supplier is already added to product's suppliers list.
                int result = ProductSupplierDB.GetId(currentProductID, currentSupID);

                if (result < 0) // If supplier is not in product's supplier list.
                {
                    try
                    {
                        // Add supplier to current product, reset/reload view for current product
                        // and notify user that supplier was added successfully.
                        ProductSupplierDB.AddProductSupplier(currentProductID, currentSupID);
                        loadProductSupplierData(currentProductID - 1);
                        MessageBox.Show("Supplier Added", "Success");
                    }
                    catch (Exception ex)
                    {
                        // Notify user of optimistic concurrency error or DB error.
                        MessageBox.Show("Unable to Add Supplier.\nThe Product Supplier was either removed or updated.\n\n" +
                                        ex.Message, "Unexpected Database Error");
                    }
                }
                else
                {
                    MessageBox.Show("That supplier is already added to the product", "Failed");
                }
            }
            else
            {
                MessageBox.Show("Please choose a supplier.", "No supplier selected");
            }
        }