private void LinkToExistingProduct(CustomerProductSelectionForm productSelectionForm, DialogResult dr) { List <Product> productList = new List <Product>(); Customer customer = CustomerBindingSource.Current as Customer; if (dr == DialogResult.OK) { using (UniConnection con = new UniConnection()) { productList = productSelectionForm.GetSelectedProducts(); foreach (Product product in productList) { try { dataAccess.CreateProductSelection(customer.id, product.id); //perform an insert statement } catch (Exception ex) { MessageBox.Show("An exception occured opening a connection to the database: " + ex.Message); } } LoadCustomerProducts(customer.id); dgvCustomerProduct.CurrentRow.Selected = true; MessageBox.Show("Selection made successfully...", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
//refactored private void LinkToExistingProduct_Click(object sender, EventArgs e) { CustomerProductSelectionForm productSelectionForm = new CustomerProductSelectionForm(); DialogResult dr = productSelectionForm.ShowDialog(); LinkToExistingProduct(productSelectionForm, dr); }