private void Form2_Load(object sender, EventArgs e) { //initiallize Suppliers = SupplierDB.GetAllsuppliers(); //get all of products from PRODUCTS TABLE //allPS = ProSu;//copy ProSu to allPS dgvProductSupplier.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //dgc select full roll dgvProductSupplier.DataSource = Suppliers; //display all of products and corresponding suppliers get from PRODUCTS_SUPPLIERS TABLE txtPName.Visible = false; lblSave.Visible = false; lblPName.Visible = false; btnSave.Visible = false; btnCancel.Visible = false; }
private void btnSave_Click(object sender, EventArgs e) { if (OperationSign == "Add") { if (txtPName.Text != "") { Supplier supplierIstance = new Supplier(); supplierIstance.SupName = txtPName.Text; //ProductDB.AddProduct(productIstance); int supplierid = SupplierDB.GetSupplierId(); SupplierDB.AddSupplier(supplierid, txtPName.Text); Suppliers = SupplierDB.GetAllsuppliers(); dgvProductSupplier.DataSource = Suppliers; MessageBox.Show("Success!"); txtPName.Text = ""; txtPName.Visible = false; lblSave.Visible = false; lblPName.Visible = false; btnSave.Visible = false; btnCancel.Visible = false; } else { MessageBox.Show("Supplier Name can not be empty!"); } } else if (OperationSign == "Update")// when confim button click, and OperationSign = update, update the data { // Product updateProduct = (Product)dgvProductSupplier.Rows[dgvProductSupplier.CurrentRow.Index].DataBoundItem; Supplier oldSupplier = (Supplier)dgvProductSupplier.Rows[dgvProductSupplier.CurrentRow.Index].DataBoundItem; // updateProduct.ProdName = txtPName.Text; Supplier updateSupplier = new Supplier(); updateSupplier.SupName = txtPName.Text; //MessageBox.Show(oldProduct.ProdName.ToString()); //SupplierDB.UpdateProduct(oldSupplier, updateSupplier); SupplierDB.UpdateSupplier(oldSupplier, updateSupplier); Suppliers = SupplierDB.GetAllsuppliers(); dgvProductSupplier.DataSource = Suppliers; MessageBox.Show("Success!"); txtPName.Text = ""; txtPName.Visible = false; lblSave.Visible = false; lblPName.Visible = false; btnSave.Visible = false; btnCancel.Visible = false; } else if (OperationSign == "Delete") // when confim button click, and OperationSign = delete, delete the data { string errorReference = ""; Supplier deleteSupplier = (Supplier)dgvProductSupplier.Rows[dgvProductSupplier.CurrentRow.Index].DataBoundItem; if (SupplierDB.referenceCheck(deleteSupplier.SupplierId, "SupplierContacts"))//there is a refrenect { errorReference = "SupplierContacts Table"; } if (SupplierDB.referenceCheck(deleteSupplier.SupplierId, "Products_suppliers")) { errorReference += " and Products_suppliers Table"; } if (errorReference == "") { try { SupplierDB.DeleteSupplier(deleteSupplier); Suppliers = SupplierDB.GetAllsuppliers(); dgvProductSupplier.DataSource = Suppliers; } catch { MessageBox.Show("The product you select has been correlated with one or more record. Please contact the database associate to countinue"); } finally { txtPName.Text = ""; txtPName.Visible = false; lblSave.Visible = false; lblPName.Visible = false; btnSave.Visible = false; btnCancel.Visible = false; } } else { MessageBox.Show("Selecected supplier is referenced in " + errorReference + " , delete failed please contact with DBA!"); } } }