예제 #1
0
        //If Modify Button was clicked on Form1
        private void frmAddModify_Load(object sender, EventArgs e)
        {
            if (!isAdd)   // Set up for Modify mode - use the Package passed from the last form to populate fields
            {
                // Update the title and description of the page
                lblTitle.Text = "Package Manager - Edit Package";
                lblDesc.Text  = $"Edit any details and modify product list for the current package (ID #{currentPackage.PackageId}).";

                using (travelexpertsDataContext db = new travelexpertsDataContext())
                {
                    // Grab current package ID used to create this modify page
                    int packageId = currentPackage.PackageId;
                    // Use an in-depth query to grab the info needed for the product info data grid
                    dataGridView1.DataSource = TravelExpertsQueryManager.FindProdInfoByPackage(db, currentPackage.PackageId);
                }

                // Set up a snapshot of current associated package_product_suppliers entries
                ppsSnapshot = TravelExpertsQueryManager.GetPackagesProductsSuppliersByPackageID(currentPackage.PackageId);

                // handle nullable datetime
                if (currentPackage.PkgStartDate == null)
                {
                    EmptyDateTimePicker(pkgStartDateDateTimePicker);
                }

                if (currentPackage.PkgEndDate == null)
                {
                    EmptyDateTimePicker(pkgEndDateDateTimePicker);
                }

                // Display current package information in details view
                packageBindingSource.Add(currentPackage);
            }

            else // Set up for Add mode
            {
                // Update the title and description of the page
                lblTitle.Text = "Package Manager - Add A New Package";
                lblDesc.Text  = "Add details and products for a new package.";
                EmptyDateTimePicker(pkgStartDateDateTimePicker);
                EmptyDateTimePicker(pkgEndDateDateTimePicker);
            }
        }
예제 #2
0
        // Go back to last page without saving changes. This may involve some cleaning up, depending on the mode and if products were added [Eric]
        private void btnCancel_Click(object sender, EventArgs e)
        {
            using (travelexpertsDataContext dbContext = new travelexpertsDataContext())
            {
                // First, we have to check to see if any products were added to the package before cancelling
                if (didAddProducts) // this will be true if so - no need to spend time querying the db
                {
                    // Get the current PPS entries in the database corresponsind to this package
                    List <Packages_Products_Supplier> ppsCurrent =
                        TravelExpertsQueryManager.GetPackagesProductsSuppliersByPackageID(currentPackage.PackageId);

                    // Next, we have to get the PPS entries to re-add (if they were deleted) and/or remove (if new ones were added)
                    // This is not a super efficient process, but packages shouldn't have enough products for it to make much difference
                    // First, get the ones to re-add
                    List <Packages_Products_Supplier> ppsToAdd = ppsSnapshot                                                                     // Creating a list of Package_Product_Suppliers where...
                                                                 .Where(ppsSnap => !ppsCurrent                                                   // ...for each snapshot entry, it is NOT the case that...
                                                                        .Any(ppsCurr => ppsCurr.ProductSupplierId == ppsSnap.ProductSupplierId)) // ...any current entry has that snapshot entry's ProductSupplierID
                                                                 .ToList();

                    // Next, the ones to remove
                    List <Packages_Products_Supplier> ppsToDelete = ppsCurrent                                                                      // Creating a list of Package_Product_Suppliers where...
                                                                    .Where(ppsCurr => !ppsSnapshot                                                  // ...for each current entry, it is NOT the case that...
                                                                           .Any(ppsSnap => ppsCurr.ProductSupplierId == ppsSnap.ProductSupplierId)) // ...any snapshot entry has that current entry's ProductSupplierID
                                                                    .ToList();

                    // Add the needed entries back
                    foreach (Packages_Products_Supplier ppsA in ppsToAdd)
                    {
                        // LINQ to SQL doesn't let you re-add old entity objects, so we need to create copies to add back in place
                        Packages_Products_Supplier clone = new Packages_Products_Supplier
                        {
                            PackageId         = ppsA.PackageId,
                            ProductSupplierId = ppsA.ProductSupplierId
                        };

                        dbContext.Packages_Products_Suppliers.InsertOnSubmit(clone);
                    }

                    // Delete the entries to undo
                    foreach (Packages_Products_Supplier ppsD in ppsToDelete)
                    {
                        // Deleting only works on entities from the current context, so need to grab them
                        // I'm sure this could be done in the ones-to-remove LINQ query above, but I couldn't manage it
                        Packages_Products_Supplier deleteTarget = dbContext.Packages_Products_Suppliers     // Search in the table...
                                                                  .Single(pps =>                            // ...for the one entry, with...
                                                                          pps.ProductSupplierId == ppsD.ProductSupplierId && //...the matching ProductSupplierID...
                                                                          pps.PackageId == ppsD.PackageId); //... and the matching PackageID

                        dbContext.Packages_Products_Suppliers.DeleteOnSubmit(deleteTarget);
                    }

                    // Save changes. Phew!
                    dbContext.SubmitChanges();
                }

                // One more step if in Add mode.
                // A package was inserted into the database initially (to get the add products half to work).
                // So, if cancelling, we want to delete it this created package.
                if (isAdd)
                {
                    // Delete package
                    Package packToDelete = dbContext.Packages
                                           .Single(p => p.PackageId == currentPackage.PackageId);

                    dbContext.Packages.DeleteOnSubmit(packToDelete);
                    dbContext.SubmitChanges(); // submit to the database
                }
            }

            // Exit the form
            DialogResult = DialogResult.Cancel;
            this.Close();
        }