コード例 #1
0
        private void addsupplierButton_Click(object sender, EventArgs e)
        {
            // make sure that a product  is selected first before adding a product
            int productIndex = productComboBox.SelectedIndex;
            // make sure that a product  is selected first before adding a product
            int SupplierIndex = supplierComboBox.SelectedIndex;

            // make sure that a course is selected
            if (productIndex == -1)
            {
                MessageBox.Show("Must select a product first");
                return;
            }
            if (productIndex == -1)
            {
                MessageBox.Show("Must select a supplier first");
                return;
            }

            {
                try
                {
                    using (PackageDataContext dbContext = new PackageDataContext())
                    {
                        Products_Supplier newProduct_Supplier = new Products_Supplier // create product supplier using provided data
                        {
                            ProductId = (int)productComboBox.SelectedValue,

                            SupplierId = (int)supplierComboBox.SelectedValue
                        };
                        // insert through data context object from the main form

                        dbContext.Products_Suppliers.InsertOnSubmit(newProduct_Supplier);
                        dbContext.SubmitChanges(); // submit to the database
                        var newProduct_SupplierDB = (from ps in dbContext.Products_Suppliers
                                                     orderby ps.ProductSupplierId descending
                                                     select ps).FirstOrDefault();
                        newProduct_Supplier.ProductSupplierId = newProduct_SupplierDB.ProductSupplierId;
                        ProductSupplierList.Add(newProduct_Supplier);
                        var ProductName = (from p in dbContext.Products
                                           where p.ProductId == newProduct_Supplier.ProductId
                                           select p).FirstOrDefault();
                        var SupplierName = (from ps in dbContext.Suppliers
                                            where ps.SupplierId == newProduct_Supplier.SupplierId
                                            select ps).FirstOrDefault();
                        productSupplierListbox.Items.Add($"Product Name: {ProductName.ProdName}, " +
                                                         $"Product Supplier: {SupplierName.SupName}");


                        productComboBox.SelectedIndex  = -1;
                        supplierComboBox.SelectedIndex = -1;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, ex.GetType().ToString());
                    return;
                }
            }
        }
コード例 #2
0
        private void AddSupplierButton_Click(object sender, EventArgs e)
        {
            if (isAdd)
            {
                if (
                    Validator.IsInt32(productIDTextbox) && Validator.IsNonNegativeInt(productIDTextbox) &&
                    Validator.IsInt32(supplierIDtextbox) && Validator.IsNonNegativeInt(supplierIDtextbox)

                    )
                {
                    try
                    {
                        using (ProductSuppliersDataContext dbContext = new ProductSuppliersDataContext())
                        {
                            Products_Supplier newProduct_Supplier = new Products_Supplier // create product supplier using provided data
                            {
                                ProductId = int.Parse(productIDTextbox.Text),

                                SupplierId = int.Parse(supplierIDtextbox.Text)
                            };
                            // insert through data context object from the main form

                            dbContext.Products_Suppliers.InsertOnSubmit(newProduct_Supplier);
                            dbContext.SubmitChanges(); // submit to the database
                            currentProductSupplier = newProduct_Supplier;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, ex.GetType().ToString());
                        return;
                    }


                    DialogResult = DialogResult.OK;
                }
                else // validation  failed
                {
                    return;
                }
            }
            else // it is Modify
            {
                if (Validator.IsInt32(productIDTextbox) && Validator.IsNonNegativeInt(productIDTextbox) &&
                    Validator.IsInt32(supplierIDtextbox) && Validator.IsNonNegativeInt(supplierIDtextbox)
                    )
                {
                    try
                    {
                        using (ProductSuppliersDataContext dbContext = new ProductSuppliersDataContext())
                        {
                            // get the product with Code from the current text box
                            Products_Supplier ps = dbContext.Products_Suppliers.Single(p => p.ProductSupplierId == int.Parse(productSupplierIDTextbox.Text));

                            //MessageBox.Show("Testing concurrency: update or delete current record from SSMS and click OK");

                            if (ps != null)
                            {
                                // make changes by copying values from text boxes
                                ps.ProductId  = int.Parse(productIDTextbox.Text);
                                ps.SupplierId = int.Parse(supplierIDtextbox.Text);
                                //submit
                                dbContext.SubmitChanges();
                                DialogResult = DialogResult.OK;
                            }
                        }
                    }
                    catch (ChangeConflictException)
                    {
                        MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception");
                        return;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, ex.GetType().ToString());
                        return;
                    }
                }
                else // validation failed
                {
                    return;
                }
            }
        }