private void DeleteSupplierProductToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // get the key of the current product in the data grid view
            int rowNum               = productListGridView.CurrentCell.RowIndex;
            Products_Supplier ps     = ProductSupplierList[rowNum];
            DialogResult      answer = MessageBox.Show($"Are you sure you want to delete{ps.ProductSupplierId} ?",
                                                       "Confirmation Message", MessageBoxButtons.YesNo);

            if (answer == DialogResult.Yes)
            {
                using (ProductSuppliersDataContext dbContext = new ProductSuppliersDataContext())
                {
                    try
                    {
                        //Delete and update the grid view information
                        Products_Supplier currentProductSupplier = dbContext.Products_Suppliers.SingleOrDefault(x => x.ProductSupplierId == ps.ProductSupplierId);
                        dbContext.Products_Suppliers.DeleteOnSubmit(currentProductSupplier);
                        dbContext.SubmitChanges();
                        ProductSupplierList.RemoveAt(rowNum);
                        RefreshGridView();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, ex.GetType().ToString());
                    }
                }
            }
        }
 private void RefreshGridView()
 {
     using (ProductSuppliersDataContext dbContext = new ProductSuppliersDataContext())
     {
         productListGridView.DataSource = dbContext.Products_Suppliers;
     }
 }
        private void AddPackageForm_Load(object sender, EventArgs e)
        {
            // loading the data to in the product supplier list
            using (ProductSuppliersDataContext dbContext = new ProductSuppliersDataContext())
            {
                productListGridView.DataSource = dbContext.Products_Suppliers;

                foreach (Products_Supplier ps in dbContext.Products_Suppliers)
                {
                    ProductSupplierList.Add(ps);
                }
            }
            if (isAddPackage)
            {
                packageIDTextBox.Enabled = false;
            }
        }
        private bool IsUniqueCode(TextBox productCodeTextBox)
        {
            Products_Supplier ps = null;

            using (ProductSuppliersDataContext dbContext = new ProductSuppliersDataContext())
            {
                ps = (from p in dbContext.Products_Suppliers
                      where p.ProductSupplierId == int.Parse(productCodeTextBox.Text)
                      select p).SingleOrDefault();

                //dbContext.Products.Single(p => p.ProductCode == productCodeTextBox.Text);
                if (ps != null) // there is another product with same code
                {
                    MessageBox.Show("Product code must be unique", "Entry Error");
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
        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;
                }
            }
        }