示例#1
0
        private void addPackageButton_Click(object sender, EventArgs e)
        {
            if (true) //replace true with data validation
            {
                //Create new Package with data from texctboxes
                Package newPackage = new Package
                {
                    PkgName             = pkgNameTextBox.Text,
                    PkgStartDate        = Convert.ToDateTime(pkgStartDateDateTimePicker.Text),
                    PkgEndDate          = Convert.ToDateTime(pkgEndDateDateTimePicker.Text),
                    PkgBasePrice        = Convert.ToDecimal(pkgBasePriceTextBox.Text),
                    PkgAgencyCommission = Convert.ToDecimal(pkgAgencyCommissionTextBox.Text),
                    PkgDesc             = pkgDescTextBox.Text
                };

                //used dbContext from the main form to insert new product
                mainForm.dbContext.Packages.InsertOnSubmit(newPackage);
                mainForm.dbContext.SubmitChanges();



                using (TravelExpertsLINQDataContext dbContext = new TravelExpertsLINQDataContext())
                {
                    int i = 0;
                    foreach (int item in prdList)
                    {
                        //Adds a new Product_Supplier object in the Product_Supplier Table
                        Products_Supplier newProdSuppliers = new Products_Supplier();
                        newProdSuppliers.ProductId  = prdList[i];
                        newProdSuppliers.SupplierId = suplList[i];

                        mainForm.dbContext.Products_Suppliers.InsertOnSubmit(newProdSuppliers);
                        mainForm.dbContext.SubmitChanges();

                        //Gets the Product Supplier Id that was just created after the Insertion
                        prodSuplList.Add(newProdSuppliers.ProductSupplierId);

                        //Adds a new Product_Supplier object in the Product_Supplier Table
                        Packages_Products_Supplier newPkgProdSupplier = new Packages_Products_Supplier();
                        newPkgProdSupplier.PackageId         = newPackage.PackageId;
                        newPkgProdSupplier.ProductSupplierId = prodSuplList[i];

                        mainForm.dbContext.Packages_Products_Suppliers.InsertOnSubmit(newPkgProdSupplier);
                        mainForm.dbContext.SubmitChanges();
                        i++;
                    }
                }

                DialogResult = DialogResult.OK;

                //Refresh the Grid View
            }
        }
        //Method to add / Edit supplier or product based on the mode
        private void addSuppLierButton_Click(object sender, EventArgs e)
        {
            if (supEditMode)
            {
                using (TravelExpertsLINQDataContext dbContext = new TravelExpertsLINQDataContext())
                {
                    Supplier supEdit = (from sup in dbContext.Suppliers
                                        where (sup.SupName == supToEdit)
                                        select sup).SingleOrDefault();

                    string suppName = newSuppTextBox.Text;

                    if (suppName != "" && suppName != supEdit.SupName)
                    {
                        supEdit.SupName = newSuppTextBox.Text;
                    }
                    else
                    {
                        MessageBox.Show("Supplier name cannot be empty and needs to be updated for edit");
                        return;
                    }


                    try
                    {
                        dbContext.SubmitChanges();
                        MessageBox.Show("Edited supplier saved to the List of of Suppliers");
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message);
                    }
                }
            }
            else if (supAddMode)
            {
                using (TravelExpertsLINQDataContext dbContext = new TravelExpertsLINQDataContext())
                {
                    Supplier supp = new Supplier();

                    string suppID   = newSuppIDTextBox.Text;
                    string suppName = newSuppTextBox.Text;
                    //Attempt to validate
                    if (suppID != "" && suppName != "")
                    {
                        int suppIDValue = Convert.ToInt32(newSuppIDTextBox.Text);
                        supp.SupplierId = suppIDValue;
                        supp.SupName    = suppName;
                    }
                    else
                    {
                        MessageBox.Show("You need to enter a Supplier Name and ID");
                        return;
                    }
                    dbContext.Suppliers.InsertOnSubmit(supp);

                    try
                    {
                        dbContext.SubmitChanges();
                        MessageBox.Show("Supplier Succesfuly Added to the List of of Suppliers");
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message);
                        return;
                    }

                    //Altering the Product_Supplier Database

                    Products_Supplier prodSup   = new Products_Supplier();
                    String            prodSupID = newSuppIDTextBox.Text;
                    int prodSuppIDValue         = Convert.ToInt32(newSuppIDTextBox.Text);

                    //Attempt to validate
                    if (prodSupID != "")
                    {
                        prodSup.SupplierId = supp.SupplierId;
                        prodSup.ProductId  = prodValue;
                    }
                    else
                    {
                        MessageBox.Show("You need to enter a valid Product Id");
                        return;
                    }

                    dbContext.Products_Suppliers.InsertOnSubmit(prodSup);

                    try
                    {
                        dbContext.SubmitChanges();
                        MessageBox.Show("Succesfuly updated the Product Suppliers Table");
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message);
                    }
                }
            }
            else if (prodEditMode)
            {
                using (TravelExpertsLINQDataContext dbContext = new TravelExpertsLINQDataContext())
                {
                    Product prodEdit = (from prod in dbContext.Products
                                        where (prod.ProdName == prodToEdit)
                                        select prod).SingleOrDefault();

                    string prodEditName = newSuppTextBox.Text;

                    if (prodEditName != "" && prodEditName != prodEdit.ProdName)
                    {
                        prodEdit.ProdName = prodEditName;
                    }
                    else
                    {
                        MessageBox.Show("Product name cannot be empty and needs to be updated for edit");
                        return;
                    }

                    try
                    {
                        dbContext.SubmitChanges();
                        MessageBox.Show("Product Succesfuly Edited");
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message);
                        return;
                    }
                }
            }
            else if (prodAddMode)
            {
                using (TravelExpertsLINQDataContext dbContext = new TravelExpertsLINQDataContext())
                {
                    Product prod     = new Product();
                    string  prodName = newSuppTextBox.Text;

                    //Attempt to validate
                    if (prodName != "")
                    {
                        prod.ProdName = prodName;
                    }
                    else
                    {
                        MessageBox.Show("You need to enter a valid Product Name");
                        return;
                    }
                    dbContext.Products.InsertOnSubmit(prod);
                    try
                    {
                        dbContext.SubmitChanges();
                        MessageBox.Show("Product Succesfuly Added to the List of of Products");
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message);
                    }
                }
            }
        }
 private void detach_Products_Suppliers(Products_Supplier entity)
 {
     this.SendPropertyChanging();
     entity.Supplier = null;
 }
 private void attach_Products_Suppliers(Products_Supplier entity)
 {
     this.SendPropertyChanging();
     entity.Product = this;
 }
 partial void DeleteProducts_Supplier(Products_Supplier instance);
 partial void UpdateProducts_Supplier(Products_Supplier instance);
 partial void InsertProducts_Supplier(Products_Supplier instance);