/// <summary> /// Get all the datat from the database /// </summary> private void getAll() { try { packages = PackagesDB.GetAllPackages(); packages2 = PackagesDB.GetAllPackages(); //Two controls cannot share array data binding products = ProductDB.GetAllProducts(); products2 = ProductDB.GetAllProducts(); //Two controls cannot share array data binding suppliers = SuppliersDB.GetAllSuppliers(); products_suppliers = Products_SupplierDB.GetAllProduct_Suppliers(); } catch (Exception ex) //Any Errors form the database read? { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
//On accept button click private void btnAccept_Click(object sender, EventArgs e) { if (IsValidData()) { if (addProducts) //add products mode { product = new Products(); Suppliers supplier = suppliers[cmbSuppliers.SelectedIndex]; this.PutProductDataAdd(product, supplier); try { product.ProductId = ProductDB.AddProducts(product); products_supplier.ProductSupplierId = Products_SupplierDB.AddProduct_Supplier(product, supplier); this.DialogResult = DialogResult.OK; } catch (SqlException ex) {//if sql error occoured switch (ex.Errors[0].Number) { case 2627: //if primary key error MessageBox.Show("You already added this Product", "Duplicate Error"); break; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else if (editProdSup) //edit product supplier mode { if (rdAddNewProd.Checked == true) //if add product within this mode { product = new Products(); Suppliers supplier = suppliers[cmbSuppliers.SelectedIndex]; this.PutProductDataAdd(product, supplier); try { //Add the products product.ProductId = ProductDB.AddProducts(product); products_supplier.ProductSupplierId = Products_SupplierDB.AddProduct_Supplier(product, supplier); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else { //select from the combobox product = products[cmbSelectProd.SelectedIndex]; Suppliers supplier = suppliers[cmbSuppliers.SelectedIndex]; this.PutProductDataAdd(product, supplier); try { products_supplier.ProductSupplierId = Products_SupplierDB.AddProduct_Supplier(product, supplier); this.DialogResult = DialogResult.Yes; } catch (SqlException ex) { switch (ex.Errors[0].Number) { case 2627: //Primary key violation MessageBox.Show(products_supplier.ProdName + " is already supplied by " + products_supplier.SupName, "Duplicate Error"); break; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } else //pure edit product mode { Products newProduct = new Products(); newProduct.ProductId = product.ProductId; this.PutProductDataEdit(newProduct); try { //Try to edit data if (!ProductDB.UpdateProduct(product, newProduct)) { MessageBox.Show("Another user has updated or " + "deleted that product.", "Database Error"); this.DialogResult = DialogResult.Retry; } else { //edit returns product = newProduct; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
private void frmUpdate_Products_Sup_Load(object sender, EventArgs e) { products_Supplier = Products_SupplierDB.GetProducts_Suppliers(); pSDataGridView.DataSource = products_Supplier; }