// gets the Product supplier combo ID
        private int GetPSID(int prodID, int suppID)
        {
            int psID = -1;

            try
            {
                using (TravelExpertDataDataContext dbContext = new TravelExpertDataDataContext())
                {
                    // get values for supplier and product IDs to find the matching PSID.
                    int productID  = Convert.ToInt32(cbProducts.SelectedValue);
                    int supplierID = Convert.ToInt32(cbSuppliers.SelectedValue);

                    // find the correct PS ID to add into the PPS table.
                    Products_Supplier psIDQuery = (from ps in dbContext.Products_Suppliers
                                                   where (ps.ProductId == productID &&
                                                          ps.SupplierId == supplierID)
                                                   select ps
                                                   ).Single();
                    psID = psIDQuery.ProductSupplierId;
                    return(psID);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());
                return(psID);
            }
        }
Exemplo n.º 2
0
        /***************************************/
        /* Product(s) of Supplier CRUD Buttons */
        /***************************************/
        private void btnAddProd_Click(object sender, EventArgs e)
        {
            // get the current product ID, Name and supplier ID, Name
            int    rowNum         = Convert.ToInt32(dgvSupplier.CurrentCell.RowIndex);
            int    currentSupId   = Convert.ToInt32(dgvSupplier["dataGridViewTextBoxColumn1", rowNum].Value);
            string currentSupName = (dgvSupplier["dataGridViewTextBoxColumn2", rowNum].Value).ToString();
            //string currentSupName = GetSupplierName(currentSupId);

            // obtain current product ID form comboBox
            int currentProdId = Convert.ToInt32(cbAddProd.SelectedValue);

            // check if the current supplier already has current product in products_suppliers table
            using (TravelExpertDataDataContext dbContext = new TravelExpertDataDataContext())
            {
                var CheckIfExists = (from ps in dbContext.Products_Suppliers
                                     where ps.ProductId == currentProdId && ps.SupplierId == currentSupId
                                     select ps).FirstOrDefault();

                //  if the record in the database already, refresh the 2 supplier list and return
                if (CheckIfExists != null)
                {
                    MessageBox.Show("The Supplier has this Product already, please check it again!");
                    RefreshPordOfSuppAndLists(currentSupId);
                    return;
                }
            }

            //  insert the new record to the database
            try
            {
                using (TravelExpertDataDataContext dbContext = new TravelExpertDataDataContext())
                {
                    Products_Supplier newProdSupp = new Products_Supplier
                    {
                        ProductId  = currentProdId,
                        SupplierId = currentSupId
                    };
                    dbContext.Products_Suppliers.InsertOnSubmit(newProdSupp);
                    dbContext.SubmitChanges();

                    //  refresh the product of supplier dataGridView and the two comboBoxes
                    RefreshPordOfSuppAndLists(currentSupId);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());
            }
        }
Exemplo n.º 3
0
        //  add a new Product_supplier relation to the database
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //  if the current product ID is zero, give warning message and return
            if (nProductID == 0)
            {
                MessageBox.Show("Please select a product from the list first!");
                return;
            }

            //  if the current supplier ID is zero, give warning message and return
            if (nSupplierID == 0)
            {
                MessageBox.Show("Please select a supplier from the right-hand side list first!");
                return;
            }

            //  check if the record in the database already
            var objCheckDup = (from ps in dbContext.Products_Suppliers
                               where ps.ProductId == nProductID && ps.SupplierId == nSupplierID
                               select ps).FirstOrDefault();

            //  if the record in the database already, refresh the 2 supplier list and return
            if (objCheckDup != null)
            {
                MessageBox.Show("Someone has added this supplier already, please check it again!");
                DisplaySupplierOnProduct(nProductID);
                return;
            }

            //  insert the new record to the database
            Products_Supplier objProdSupp = new Products_Supplier
            {
                ProductId  = nProductID,
                SupplierId = nSupplierID
            };

            dbContext.Products_Suppliers.InsertOnSubmit(objProdSupp);
            dbContext.SubmitChanges();

            //  refresh the 2 supplier list
            DisplaySupplierOnProduct(nProductID);
        }
 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);