Example #1
0
        //Adding new package
        private void btnAdd_Click(object sender, EventArgs e)
        {
            HideForm();
            //create second form
            frmAddModifyPackage addModPkg = new frmAddModifyPackage();

            //create products form
            frmAddMultiProd addProd = new frmAddMultiProd();

            // setting isAdd to true to pass it to the second form
            addModPkg.isAdd   = true;
            addModPkg.package = null; //no package

            //show it modal
            DialogResult result = addModPkg.ShowDialog();//accept returns ok

            //if dialogresult is ok, save package, and display items in list view
            if (result == DialogResult.OK)
            {
                selectedPackage     = addModPkg.package;
                selectedProductsIds = addModPkg.updated_Product_Selections;
                try
                {
                    var newPackage = context.Packages.Add(selectedPackage);
                    context.SaveChanges();
                    //adding associated products only if added
                    if (selectedProductsIds != null)
                    {
                        foreach (var item in selectedProductsIds)
                        {
                            //creating the corresponding entry for each selected product
                            //in the PackagesProductsSuppliers table
                            PackagesProductsSuppliers pkgProdsup = new PackagesProductsSuppliers();
                            pkgProdsup.ProductSupplierId = item;
                            int id = selectedPackage.PackageId;
                            pkgProdsup.PackageId = id;
                            context.PackagesProductsSuppliers.Add(pkgProdsup);
                        }
                        context.SaveChanges();
                    }


                    DisplayLVPackages();
                }
                catch (DbUpdateException ex)
                {
                    HandleDataError(ex);
                }
                catch (Exception ex)
                {
                    HandleGeneralError(ex);
                }
            }
            ShowForm();
            ClearSelection();
        }
Example #2
0
        //Modifying existing product
        private void btnModify_Click(object sender, EventArgs e)
        {
            HideForm();
            //create second form
            frmAddModifyPackage addModPkg = new frmAddModifyPackage();

            // setting isAdd to false to pass it to the second form
            addModPkg.isAdd = false;

            /*retrieving the selected product
             * selected_package code is retrieved from the lvPackages_ItemSelectionChanged
             * event handler*/
            addModPkg.package = context.Packages.Find(selected_packageID);
            addModPkg.Original_Product_selections = ProductsByPackage(selected_packageID);

            //show it modal
            DialogResult result = addModPkg.ShowDialog();//accept returns ok

            //if dialogresult is ok, save product, and display items in list view
            if (result == DialogResult.OK)
            {
                selectedPackage = addModPkg.package;
                var updated  = addModPkg.updated_Product_Selections;
                var original = GetProdSuppId_Selections(selected_packageID);
                if (updated != null)
                {
                    selectedProductsIds = updated;
                }
                else
                {
                    selectedProductsIds = original;
                }
                try
                {
                    //remove old selections
                    var pkgProdSuppliers = context.PackagesProductsSuppliers
                                           .Where(p => p.PackageId == selected_packageID);


                    foreach (var item in pkgProdSuppliers)
                    {
                        context.PackagesProductsSuppliers.Remove(item);
                    }

                    context.SaveChanges();

                    //add new selections

                    foreach (var item in selectedProductsIds)
                    {
                        //creating the corresponding entry for each selected product
                        //in the PackagesProductsSuppliers table
                        PackagesProductsSuppliers pkgProdsup = new PackagesProductsSuppliers();
                        pkgProdsup.ProductSupplierId = item;
                        int id = selectedPackage.PackageId;
                        pkgProdsup.PackageId = id;
                        context.PackagesProductsSuppliers.Add(pkgProdsup);
                    }

                    context.SaveChanges();
                    DisplayLVPackages();
                }



                catch (DbUpdateException ex)
                {
                    HandleDataError(ex);
                }
                catch (Exception ex)
                {
                    HandleGeneralError(ex);
                }
            }
            ShowForm();
            ClearSelection();
            ManageControls(false);
        }