Beispiel #1
0
        //populate combo boxes so there is a supplierID and product ID to choose from
        //Populate the Product list with products from the database
        //Populate the Supplier list with supplier from the database
        private void FillComboBoxes()
        {
            // return list of productID created in GetProductName()
            List <Product> myProdList = ProductsDB.GetProducts();

            // adding product names to the CBName (combo box)
            var prodLinq = from prod in myProdList
                           select new
            {
                prod.ProductId
            };

            foreach (var item in prodLinq)
            {
                cboProdID.Items.Add(item.ProductId);
            }

            // return list of supplierID names created in GetProductName()
            List <Supplier> mySupList = SuppliersDB.GetSuppliers();

            // adding package names to the CBName (combo box)
            var supLinq = from sup in mySupList
                          select new
            {
                sup.SupplierId
            };

            foreach (var item in supLinq)
            {
                cboSupID.Items.Add(item.SupplierId);
            }
        }
        private void PutSupplierData(Supplier newsupplier)
        {
            try
            {
                if (Validator1.IsProvided(txtSupId, "Supplier Id") &&
                    Validator1.IsProvided(txtSupName, "Supplier Name") &&
                    Validator1.IsNonNegativeDecimal(txtSupId, "Supplier Id"))
                {
                    supplier.SupplierId = Convert.ToInt32(txtSupId.Text);
                    supplier.SupName    = txtSupName.Text.ToString();

                    SuppliersDB.AddNewSupplier(supplier);

                    this.DialogResult = DialogResult.OK;
                    MessageBox.Show("Supplier has been saved");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());
            }
        }
        // populate drop down with names of sup objects from the Suppliers list
        private void PopulateSuppliers()
        {
            // return list of suppliers created in GetSuppliers()
            List <Supplier> supplierList = SuppliersDB.GetSuppliers();
            //if (btnUpdateClicked)
            // {
            // adding supplier names to the CBName (combo box)
            var supplierLinq = from sup in supplierList
                               select new
            {
                sup.SupName
            };

            foreach (var item in supplierLinq)
            {
                cBName.Items.Add(item.SupName);
            }
            // }
            // else
            // {
            //// adding supplier names to the CBName (combo box)
            //var supplierLinq = from sup in supplierList
            //                   select new
            //                   {
            //                       sup.SupplierId
            //                   };

            //foreach (var item in supplierLinq)
            //{
            //    cbSupID.Items.Add(item.SupplierId);
            //}


            cbSupID.DataSource    = supplierList;
            cbSupID.DisplayMember = "SupName";
            cbSupID.ValueMember   = "SupplierId";
            //}
        }
        //Saving the Data in database
        private void btnSave_Click(object sender, EventArgs e)
        {
            bool updated = false;

            //if Supplier button is clicked
            if (btnSupClicked)
            {
                //Validating Supplier Table
                if (Validator1.IsProvidedCombo(cBName, "Supplier Name"))
                {
                    Supplier newSup = new Supplier();
                    newSup.SupName = cBName.Text;  //for new supplier name

                    updated = SuppliersDB.UpdateSupplier(newSup, SingleSup);
                    if (updated)
                    {
                        MessageBox.Show("Supplier Updated");
                    }
                    else
                    {
                        MessageBox.Show("Supplier not Updated. Please try again.");
                    }
                }
            }
            //If Products button is clicked
            if (btnProdClicked)
            {
                //Validating Product Table
                if (Validator1.IsProvidedCombo(cBName, "Product Name"))
                {
                    Product NewProd = new Product();
                    NewProd.ProdName = cBName.Text; //for new Product name

                    updated = ProductsDB.UpdateProducts(NewProd, SingleProd);
                    if (updated)
                    {
                        MessageBox.Show("Products Updated");
                    }
                    else
                    {
                        MessageBox.Show("Products not Updated. Please try again");
                    }
                }
            }
            if (btnProdSupClicked)// this does not work yet
            {
                //Validating Product-Supplier table
                if (Validator1.IsProvidedCombo(cbProdID, "Product Id") &&
                    Validator1.IsProvidedCombo(cbSupID, "Supplier Id"))
                {
                    ProdSupplier newProdSupplier = new ProdSupplier();
                    newProdSupplier.ProductId  = Convert.ToInt32(cbProdID.SelectedValue);
                    newProdSupplier.SupplierId = Convert.ToInt32(cbSupID.SelectedValue);

                    updated = ProdSuppliersDB.UpdateProdSupplier(newProdSupplier, SingleProdSup);
                    if (updated)
                    {
                        MessageBox.Show("Product_Supplier is Updated");
                    }
                    else
                    {
                        MessageBox.Show("Product_Supplier is not Updated. Please try again");
                    }
                }
            }
            if (btnPackClicked)  // Otherwise just load Packages
            {
                //Validating data for Packages
                if (Validator1.IsProvidedCombo(cBName, "Package Name") &&
                    Validator1.IsProvided(txtDesc, "Describtion") &&
                    Validator1.IsProvided(txtBasePrice, "Base Price") &&
                    Validator1.IsProvided(txtAgencyComm, "Agency Commission"))
                {
                    Package NewPackage = new Package();
                    NewPackage.PkgName = cBName.Text;

                    //Start Date cannnot be greater then End Date
                    if (dTPStartDate.Value.Date >= dTPEndDate.Value.Date)
                    {
                        MessageBox.Show("Package start date cannot be later than end date");
                    }

                    //Agency commission cannot be greater then base price
                    else if (Convert.ToDecimal(txtBasePrice.Text.Replace("$", "")) < (Convert.ToDecimal(txtAgencyComm.Text.Replace("$", ""))))
                    {
                        MessageBox.Show("Agency Comission cannot be more than Package Base price!");
                    }
                    else
                    {
                        NewPackage.PkgStartDate        = dTPStartDate.Value.ToString("yyyy-MM-dd");
                        NewPackage.PkgEndDate          = dTPEndDate.Value.ToString("yyyy-MM-dd");
                        NewPackage.PkgDesc             = txtDesc.Text;
                        NewPackage.PkgBasePrice        = Convert.ToDecimal(txtBasePrice.Text.Replace("$", ""));
                        NewPackage.PkgAgencyCommission = Convert.ToDecimal(txtAgencyComm.Text.Replace("$", ""));

                        updated = PackageDB.UpdatePackage(NewPackage, SinglePkg);
                    }

                    if (updated)
                    {
                        MessageBox.Show("Package is Updated");
                    }
                    else
                    {
                        MessageBox.Show("Package is not Updated, Please try again.");
                    }
                }
            }
        }
        // when name combo box (ddl) is used to select an object from the list
        private void cBPkgName_SelectedIndexChanged(object sender, EventArgs e)
        {
            // if the Products button was most recently clicked
            if (btnProdClicked)
            {
                // return list of packages created in GetProducts()
                List <Product> productList = ProductsDB.GetProducts();

                // display information about the selected product
                if (cBName.SelectedIndex != -1)
                {
                    var prod = (from selectedprod in productList where
                                selectedprod.ProdName == cBName.Text
                                select selectedprod).First();

                    txtID.Text = prod.ProductId.ToString();

                    //Maryam
                    SingleProd = prod;
                }
            }

            // if the Suppliers button was most recently clicked
            else if (btnSupClicked)
            {
                // return list of packages created in GetSuppliers()
                List <Supplier> supplierList = SuppliersDB.GetSuppliers();

                // if a selection is made from the combobox
                if (cBName.SelectedIndex != -1)
                {
                    // display information about the selected supplier
                    var sup = (from selectedsup in supplierList
                               where selectedsup.SupName == cBName.Text
                               select selectedsup).First();

                    txtID.Text = sup.SupplierId.ToString();

                    //Maryam
                    SingleSup            = new Supplier();
                    SingleSup.SupplierId = Convert.ToInt32(txtID.Text);
                    SingleSup.SupName    = cBName.Text;
                }
            }

            // if the Packages button was most recently clicked
            // or on form load
            else
            {
                // return list of packages created in GetPackages()
                List <Package> packageList = MainPackageDB.GetPackages();

                // if a selection is made from the combo box
                if (cBName.SelectedIndex != -1)
                {
                    // display information about the selected package
                    var pkg = (from selectedpkg in packageList where
                               selectedpkg.PkgName == cBName.Text
                               select selectedpkg).First();

                    txtID.Text         = pkg.PackageId.ToString();
                    dTPStartDate.Value = Convert.ToDateTime(pkg.PkgStartDate);
                    dTPEndDate.Value   = Convert.ToDateTime(pkg.PkgEndDate);
                    txtDesc.Text       = pkg.PkgDesc;
                    txtBasePrice.Text  = pkg.PkgBasePrice.ToString("c");
                    txtAgencyComm.Text = pkg.PkgAgencyCommission.ToString("c");

                    //Maryam
                    SinglePkg = pkg;
                }
            }
        }