Beispiel #1
0
        // delete Package
        private void btnDeletePackage_Click(object sender, EventArgs e)
        {
            // get selected package
            int            packageId   = Convert.ToInt32(cmbPackageId.Text);
            List <Package> packageList = PackageDB.GetPackages(packageId);
            Package        package     = packageList.First();

            // confirm to delete
            DialogResult result = MessageBox.Show("Delete Package " + package.PackageId + "?",
                                                  "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                try
                {
                    // get current product suppliers
                    List <int> productSupplierIds = new List <int>();
                    foreach (var product in ProductsInPackageDB.GetProductsFromPackage(packageId))
                    {
                        productSupplierIds.Remove(product.ProductSupplierId);
                    }

                    // delete products suppliers linked to package
                    Packages_products_suppliersDB.Delete(packageId);

                    foreach (DataGridViewRow item in this.grdProductList.SelectedRows)
                    {
                        grdProductList.Rows.RemoveAt(item.Index);
                    }

                    // delete package
                    PackageDB.DeletePackage(package);
                    MessageBox.Show("Package " + packageId + " deleted successfully");
                    tabPackageList.SelectTab(0); // go back to list view
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Beispiel #2
0
        // update Package
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            // check all input values
            if (IsValidData())
            {
                // check if package end date is greater than start date
                if (txtPkgStart.Value > txtPkgEnd.Value || txtPkgStart.Value == txtPkgEnd.Value)
                {
                    MessageBox.Show("End Date should be greater than Start Date. Please choose a valid date.", "Input Error");
                }
                else
                {
                    // check if price is greater than commission
                    if (Convert.ToDouble(txtPkgCommission.Text) > Convert.ToDouble(txtPkgPrice.Text))
                    {
                        MessageBox.Show("Commission cannot be greater than the Base Price. Please enter a valid amount.", "Input Error");
                    }
                    else
                    {
                        // get current package from database
                        if (cmbPackageId.Text == "")
                        {
                            return;
                        }

                        int            packageId      = Convert.ToInt32(cmbPackageId.Text);
                        List <Package> oldPackageList = PackageDB.GetPackages(packageId);
                        Package        oldPackage     = oldPackageList.First();

                        // set new values
                        Package newPackage = new Package();
                        newPackage.PackageId = packageId;
                        this.PutPackageData(newPackage);


                        // save package
                        try
                        {
                            package = newPackage;
                            PackageDB.UpdatePackage(oldPackage, newPackage);

                            // get current product suppliers
                            List <int> productSupplierIds = new List <int>();
                            foreach (var product in ProductsInPackageDB.GetProductsFromPackage(packageId))
                            {
                                productSupplierIds.Add(product.ProductSupplierId);
                            }

                            // delete products suppliers linked to package
                            Packages_products_suppliersDB.Delete(packageId);

                            // add products supliers to package
                            List <ProductsInPackage> productsInPackages = (List <ProductsInPackage>)productsInPackageBindingSource.DataSource;
                            foreach (var productsInPackage in productsInPackages)
                            {
                                Packages_products_suppliersDB.Add(packageId, productsInPackage.ProductSupplierId);
                            }

                            MessageBox.Show("Changes saved for Package ID " + packageId);
                            tabPackageList.SelectTab(0); // go back to list view
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
        }