// class to delete a prodoctsupplier from the database public static bool DeleteProdSup(Products_Suppliers prodsup) { SqlConnection connection = TravelExpertsDB.GetConnection(); string deleteStatement = "DELETE FROM Products_Suppliers WHERE ProductSupplierId = @ProductSupplierId"; SqlCommand deleteCommand = new SqlCommand(deleteStatement, connection); deleteCommand.Parameters.AddWithValue("@ProductSupplierId", prodsup.productsupplierid); try { connection.Open(); int count = deleteCommand.ExecuteNonQuery(); if (count > 0) { return(true); } else { return(false); } } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
public static List <Products_Suppliers> GetProductsBySupplierName(string supplierName) { List <Products_Suppliers> supplier_products = new List <Products_Suppliers>(); SqlConnection connection = TravelExpertsDB.GetConnection(); string selectStatement = "SELECT ProdName, SupName " + "FROM Products prd JOIN Products_Suppliers prdSpp ON prd.ProductId=prdSpp.ProductId " + "JOIN Suppliers spp ON spp.SupplierId=prdSpp.SupplierId WHERE spp.SupName=@SupplierName"; SqlCommand command = new SqlCommand(selectStatement, connection); command.Parameters.AddWithValue("@SupplierName", supplierName); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Products_Suppliers prodSup = new Products_Suppliers(); prodSup.productName = reader["ProdName"].ToString(); prodSup.SupName = reader["SupName"].ToString(); supplier_products.Add(prodSup); } return(supplier_products); } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
// class to add a product_supplier to the database public static int AddProdSupp(Products_Suppliers prodSup) { SqlConnection connection = TravelExpertsDB.GetConnection(); string insertStatement = "INSERT INTO Products_Suppliers (ProductId, SupplierId) VALUES (@ProductId, @SupplierId)"; SqlCommand command = new SqlCommand(insertStatement, connection); command.Parameters.AddWithValue("@ProductId", prodSup.productid); command.Parameters.AddWithValue("@SupplierId", prodSup.supplierid); try { connection.Open(); command.ExecuteNonQuery(); string selectStatement = "SELECT IDENT_CURRENT('Products_Suppliers') FROM Products_Suppliers"; SqlCommand selectCommand = new SqlCommand(selectStatement, connection); int Products_SuppliersID = Convert.ToInt32(selectCommand.ExecuteScalar()); return(Products_SuppliersID); } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
private void btnAdd_Click(object sender, EventArgs e) { int index = cboProducts.SelectedIndex; int prodId = productsList[index].ProductId; Products_Suppliers productsSuppliers = new Products_Suppliers(); if (prodId < 1) // no selection { MessageBox.Show("Please select supplier to add"); } else // user selected a product to add { try { int ProductSupplierId = Products_SuppliersDB.AddSupplierById(prodId, (int)supId); MessageBox.Show("Product deleted", "Success!"); this.DialogResult = DialogResult.OK; Products_Suppliers ps = Products_SuppliersDB.GetProductSupplierById(ProductSupplierId); // selected product //add selected product currentProductSupplierIds.Add(ps); // add to current product supplier list loadData(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } //DisplayCurrentPackageProductSupplierData(); } }
// class to return a list of all records in the table public static List <Products_Suppliers> GetAllProdSup() { List <Products_Suppliers> product_suppliers = new List <Products_Suppliers>(); SqlConnection connection = TravelExpertsDB.GetConnection(); string selectStatement = "SELECT * FROM Products_Suppliers"; SqlCommand selectCommand = new SqlCommand(selectStatement, connection); try { connection.Open(); SqlDataReader reader = selectCommand.ExecuteReader(); while (reader.Read()) { Products_Suppliers prodsup = new Products_Suppliers(); prodsup.productsupplierid = (int)reader["ProductSupplierId"]; prodsup.productid = (int)reader["ProductId"]; prodsup.supplierid = (int)reader["SupplierId"]; product_suppliers.Add(prodsup); } return(product_suppliers); } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
// ADD a supplier to current product click private void btnAdd_Click(object sender, EventArgs e) { int index = cboSupplier.SelectedIndex; int supId = suppliersList[index].SupplierId; Products_Suppliers productsSuppliers = new Products_Suppliers(); if (supId < 1) { MessageBox.Show("Please select supplier to add"); } else { try { int ProductSupplierId = Products_SuppliersDB.AddSupplierById((int)prodId, supId); MessageBox.Show("Supplier added", "Success!"); this.DialogResult = DialogResult.OK; Products_Suppliers ps = Products_SuppliersDB.GetProductSupplierById(ProductSupplierId); currentProductSupplierIds.Add(ps); DisplayProductSupplier(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
private void btnAdd_Click(object sender, EventArgs e) { if (lstProdSup.SelectedIndex < 0) // no selection { MessageBox.Show("Please select product supplier to add"); } else { Packages currentPackage = PackagesDB.GetPackageById((int)id); string i = lstProdSup.SelectedItem.ToString(); string[] s = i.Split('|'); int prodSupId = Int32.Parse(s[0].Trim()); Products_Suppliers selectedProductSupplier = Products_SuppliersDB.GetProductSupplierById(prodSupId); try { if (!Packages_Products_SuppliersDB.AddPackageProductSupplier(currentPackage.PackageId, selectedProductSupplier.ProductSupplierId)) { MessageBox.Show("Product added", "Success!"); this.DialogResult = DialogResult.OK; DisplayPackage(); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
// DELETE a supplier to current product click private void btnRemove_Click(object sender, EventArgs e) { string i = lstSuppliers.SelectedItem.ToString(); string[] s = i.Split('|'); int prodSupId = Int32.Parse(s[0].Trim()); Products_Suppliers productsSuppliers = new Products_Suppliers(); if (prodSupId < 1) // no selection { MessageBox.Show("Please select supplier to add"); } else // user selected a product to add { try { this.DialogResult = DialogResult.OK; Products_Suppliers ps = Products_SuppliersDB.GetProductSupplierById(prodSupId); Products_SuppliersDB.DeleteProductSupplier(ps); MessageBox.Show("Supplier deleted", "Success!"); loadData(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
private void DisplayProductSupplierData() { productSupplierIds = Products_SuppliersDB.GetProductSupplierIds(); lstProdSup.Items.Clear(); lstProdSup.Items.Add("Id " + ": " + "Product Name" + ", " + "Supplier Name"); foreach (int id in productSupplierIds) { Products_Suppliers ps = Products_SuppliersDB.GetProductSupplierById(id); lstProdSup.Items.Add(ps); } }
private void btnAdd_Click(object sender, EventArgs e) { string tableName = cboTableNames.SelectedValue.ToString(); if (tableName == "Products") { frmAddUpdateProducts addProductForm = new frmAddUpdateProducts(); addProductForm.addProduct = true; DialogResult result = addProductForm.ShowDialog(); if (result == DialogResult.OK) { currentProd = addProductForm.product; this.DisplayProductData(); } } else if (tableName == "Suppliers") { frmAddUpdateSuppliers addSupplierForm = new frmAddUpdateSuppliers(); addSupplierForm.addSupplier = true; DialogResult result = addSupplierForm.ShowDialog(); if (result == DialogResult.OK) { currentSupplier = addSupplierForm.supplier; this.DisplaySupplierData(); } } else if (tableName == "Products_Suppliers") { frmAddUpdateProductSupplier addProductSupplierForm = new frmAddUpdateProductSupplier(); addProductSupplierForm.addProductSupplier = true; DialogResult result = addProductSupplierForm.ShowDialog(); if (result == DialogResult.OK) { currentProductSupplier = addProductSupplierForm.productSupplier; this.DisplayProductSupplierData(); } } else if (tableName == "Packages") { frmAddUpdatePackages addPackageForm = new frmAddUpdatePackages(); addPackageForm.addPackage = true; DialogResult result = addPackageForm.ShowDialog(); if (result == DialogResult.OK) { currentPackage = addPackageForm.package; this.DisplayPackages(); this.DisplayProductSupplierData(); } } }
private void btnDelete_Click(object sender, EventArgs e) { int index = lstPackProdSupp.SelectedIndex; string i = lstPackProdSupp.SelectedItem.ToString(); string[] s = i.Split('|'); int pid = Int32.Parse(s[0].Trim()); //Packages_Products_Suppliers currentPackage = Packages_Products_SuppliersDB.GetPackageIds if (pid < 1) // no selection { MessageBox.Show("Please select product supplier to delete"); } else // user selected a product to delete { if (type == "View") { Products_Suppliers pps = Products_SuppliersDB.GetProductSupplierById(pid); // selected product DialogResult answer = MessageBox.Show("Are you sure to delete " + pps.ProdName + "?", "Please Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (answer == DialogResult.Yes) { //delete selected package product supplier try { if (!Packages_Products_SuppliersDB.DeletePackageProductSupplier((int)id, pps.ProductSupplierId)) { MessageBox.Show("Another user has updated or deleted " + "that product.", "Database Error"); } else { currentProductSupplierIds.RemoveAt(index - 1); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } // remove from the current product supplier list DisplayCurrentPackageProductSupplierData(); } } } }
private void DisplayCurrentPackageProductSupplierData() { if (productSupplierIds != null) // if we have product suppliers to display { lstProductSupplierId.Items.Clear(); //start with empty list box lstProductSupplierId.Items.Add("Id " + ": " + "Product Name" + ", " + "Supplier Name"); foreach (int id in productSupplierIds) { Products_Suppliers ps = Products_SuppliersDB.GetProductSupplierById(id); lstProductSupplierId.Items.Add(ps); } } else // null this product does not exist - need to refresh combo box { packageIds = Packages_Products_SuppliersDB.GetPackageIds(); } }
// display ProductSupplier attached to package by DB method, passing through the packageID inherited from main form private void DisplayCurrentPackageProductSupplierData() { if (currentProductSupplierIds != null) // if we have product suppliers to display { lstPackProdSupp.Items.Clear(); //start with empty list box lstPackProdSupp.Items.Add("Id " + ": " + "Product Name" + ", " + "Supplier Name"); foreach (int id in currentProductSupplierIds) { Products_Suppliers ps = Products_SuppliersDB.GetProductSupplierById((int)id); lstPackProdSupp.Items.Add(ps); } } else { lstPackProdSupp.Items.Clear(); } }
private void btnAccept_Click(object sender, EventArgs e) { if (addProductSupplier) { productSupplier = new Products_Suppliers(); this.PutProductSupplierData(productSupplier); try { productSupplier.ProductSupplierId = Products_SuppliersDB.AddProductSupplier(productSupplier); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else { Products_Suppliers newProductSupplier = new Products_Suppliers(); newProductSupplier.ProductSupplierId = productSupplier.ProductSupplierId; this.PutProductSupplierData(newProductSupplier); try { if (!Products_SuppliersDB.UpdateProductSupplier(productSupplier, newProductSupplier)) { MessageBox.Show("Another user has updated or " + "deleted that product supplier.", "Database Error"); this.DialogResult = DialogResult.Retry; } else { productSupplier = newProductSupplier; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
// class to get a product_supplier record public static Products_Suppliers GetProdSup(int productSupplierId) { // set up the connection SqlConnection connection = TravelExpertsDB.GetConnection(); // sql statement string selectStatement = "SELECT ProductSupplierId, ProductId, SupplierId FROM Products_Suppliers WHERE ProductSupplierId = @ProductSupplierId"; SqlCommand command = new SqlCommand(selectStatement, connection); command.Parameters.AddWithValue("@ProductSupplierId", productSupplierId); // use a try catch to attampt to get the data try { connection.Open(); SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow); if (reader.Read()) { Products_Suppliers prodSup = new Products_Suppliers(); prodSup.productsupplierid = (int)reader["ProductSupplierId"]; prodSup.productid = (int)reader["ProductId"]; prodSup.supplierid = (int)reader["SupplierId"]; return(prodSup); } else { return(null); } } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
// class to update a producxt supplier to the database public static bool UpdateProdSup(Products_Suppliers oldProdSup, Products_Suppliers newProdSup) { // set up connection SqlConnection connection = TravelExpertsDB.GetConnection(); string updateStatement = "UPDATE Products_Suppliers SET " + "ProductId = @newProductId, SupplierId = @newSupplierId WHERE ProductSupplierId = @oldProductSupplierId"; SqlCommand updateCommand = new SqlCommand(updateStatement, connection); // updateCommand.Parameters.AddWithValue("@newProductSupplierId", newProdSup.productsupplierid); updateCommand.Parameters.AddWithValue("@newProductId", newProdSup.productid); updateCommand.Parameters.AddWithValue("@newSupplierId", newProdSup.supplierid); updateCommand.Parameters.AddWithValue("@oldProductSupplierId", oldProdSup.productsupplierid); try { connection.Open(); int count = updateCommand.ExecuteNonQuery(); if (count > 0) { return(true); } else { return(false); } } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
// button to add a product to the supplier private void btnProduct_Click(object sender, EventArgs e) { if (cmbProductBName.IsPresent()) { // for adding a supplier if (add) { Products_Suppliers product = new Products_Suppliers(); product.productid = (int)cmbProductBName.SelectedValue; product.productName = cmbProductBName.Text; productSuppliers.Add(product); RedisplayList(); cmbProductBName.SelectedIndex = -1; } //for modifing a supplier else { Products_Suppliers product = new Products_Suppliers(); product.productid = (int)cmbProductBName.SelectedValue; product.productName = cmbProductBName.Text; product.supplierid = supplier.SupplierId; try { Products_SuppliersDB.AddProdSupp(product); productSuppliers = Products_SuppliersDB.GetAllProdSupOnID(supplierContact.SupplierId); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } RedisplayList(); cmbProductBName.SelectedIndex = -1; } } }
// class to return a list of all records in the table based on productid public static List <Products_Suppliers> GetAllProdSupOnProdID(int productid) { List <Products_Suppliers> product_suppliers = new List <Products_Suppliers>(); SqlConnection connection = TravelExpertsDB.GetConnection(); string selectStatement = "SELECT * FROM Products_Suppliers ps join Suppliers s on ps.SupplierId = s.SupplierId WHERE ProductId = @ProductId"; SqlCommand selectCommand = new SqlCommand(selectStatement, connection); selectCommand.Parameters.AddWithValue("@ProductId", productid); try { connection.Open(); SqlDataReader reader = selectCommand.ExecuteReader(); while (reader.Read()) { Products_Suppliers prodsup = new Products_Suppliers(); prodsup.productsupplierid = (int)reader["ProductSupplierId"]; prodsup.productid = (int)reader["ProductId"]; prodsup.supplierid = (int)reader["SupplierId"]; prodsup.SupName = reader["SupName"].ToString(); product_suppliers.Add(prodsup); } return(product_suppliers); } catch (SqlException ex) { throw ex; } finally { connection.Close(); } }
private void PutProductSupplierData(Products_Suppliers productSupplier) { productSupplier.ProductId = (int)cboProdId.SelectedValue; productSupplier.SupplierId = (int)cboSupplierId.SelectedValue; }
private void btnDelete_Click(object sender, EventArgs e) { string tableName = cboTableNames.SelectedValue.ToString(); if (tableName == "Products") { DialogResult result = MessageBox.Show("Delete " + currentProd.ProdName + "?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { if (!ProductsDB.DeleteProduct(currentProd)) { MessageBox.Show("Another user has updated or deleted " + "that product.", "Database Error"); currentProd = ProductsDB.GetProductById(currentProd.ProductId); if (currentProd != null) { this.DisplayProductData(); } } else { this.DisplayProductData(); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } else if (tableName == "Suppliers") { DialogResult result = MessageBox.Show("Delete " + currentSupplier.SupName + "?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { if (!SuppliersDB.DeleteSupplier(currentSupplier)) { MessageBox.Show("Another user has updated or deleted " + "that supplier.", "Database Error"); currentSupplier = SuppliersDB.GetSupplierById(currentSupplier.SupplierId); if (currentSupplier != null) { this.DisplaySupplierData(); } } else { this.DisplaySupplierData(); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } else if (tableName == "Products_Suppliers") { DialogResult result = MessageBox.Show("Delete Product Supplier " + currentProductSupplier.ProductSupplierId + "?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { if (!Products_SuppliersDB.DeleteProductSupplier(currentProductSupplier)) { MessageBox.Show("Another user has updated or deleted " + "that product_supplier.", "Database Error"); currentProductSupplier = Products_SuppliersDB.GetProductSupplierById(currentProductSupplier.ProductSupplierId); if (currentProductSupplier != null) { this.DisplayCurrentProductSupplierData(); } this.DisplayProductSupplierData(); } else { this.DisplayCurrentProductSupplierData(); } this.DisplayProductSupplierData(); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } else if (tableName == "Packages") { DialogResult result = MessageBox.Show("Delete Packages " + currentPackage.PkgName + "?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { if (!PackagesDB.DeletePackage(currentPackage)) { MessageBox.Show("Another user has updated or deleted " + "that package.", "Database Error"); currentPackage = PackagesDB.GetPackageById(currentPackage.PackageId); if (currentPackage != null) { //this.DisplayCurrentProductSupplierData(); this.DisplayProductSupplierData(); } } else { //this.DisplayCurrentProductSupplierData(); this.DisplayPackages(); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } }
private void btnUpdate_Click(object sender, EventArgs e) { string tableName = cboTableNames.SelectedValue.ToString(); if (tableName == "Products") { frmAddUpdateProducts updateProductForm = new frmAddUpdateProducts(); updateProductForm.addProduct = false; updateProductForm.product = currentProd; DialogResult result = updateProductForm.ShowDialog(); if (result == DialogResult.OK) { currentProd = updateProductForm.product; this.DisplayProductData(); } else if (result == DialogResult.Retry) { currentProd = ProductsDB.GetProductById(currentProd.ProductId); if (currentProd != null) { this.DisplayProductData(); } } } else if (tableName == "Suppliers") { frmAddUpdateSuppliers updateSupplierForm = new frmAddUpdateSuppliers(); updateSupplierForm.addSupplier = false; updateSupplierForm.supplier = currentSupplier; DialogResult result = updateSupplierForm.ShowDialog(); if (result == DialogResult.OK) { currentSupplier = updateSupplierForm.supplier; this.DisplaySupplierData(); } else if (result == DialogResult.Retry) { currentSupplier = SuppliersDB.GetSupplierById(currentSupplier.SupplierId); if (currentSupplier != null) { this.DisplaySupplierData(); } } } else if (tableName == "Products_Suppliers") { frmAddUpdateProductSupplier updateProductSupplierForm = new frmAddUpdateProductSupplier(); updateProductSupplierForm.addProductSupplier = false; updateProductSupplierForm.productSupplier = currentProductSupplier; DialogResult result = updateProductSupplierForm.ShowDialog(); if (result == DialogResult.OK) { this.DisplayCurrentProductSupplierData(); this.DisplayProductSupplierData(); } else if (result == DialogResult.Retry) { currentProductSupplier = Products_SuppliersDB.GetProductSupplierById(currentProductSupplier.ProductSupplierId); if (currentProductSupplier != null) { this.DisplayCurrentProductSupplierData(); } this.DisplayProductSupplierData(); } } else if (tableName == "Packages") { frmAddUpdatePackages updatePackageForm = new frmAddUpdatePackages(); updatePackageForm.addPackage = false; updatePackageForm.package = currentPackage; updatePackageForm.currentProductSupplierIds = Packages_Products_SuppliersDB.GetProductSupplierIds(currentPackage.PackageId); DialogResult result = updatePackageForm.ShowDialog(); if (result == DialogResult.OK) { this.DisplayPackages(); DisplayCurrentPackageProductSupplierData(); } else if (result == DialogResult.Retry) { currentPackage = PackagesDB.GetPackageById(currentPackage.PackageId); if (currentPackage != null) { DisplayCurrentPackageProductSupplierData(); } this.DisplayPackages(); } } }
private void DisplayCurrentProductSupplierData() { currentProductSupplier = Products_SuppliersDB.GetProductSupplierById(currentProductSupplier.ProductSupplierId); txtName.Text = currentProductSupplier.ProdName; txtName2.Text = currentProductSupplier.SupName; }
private void cboId_SelectedIndexChanged(object sender, EventArgs e) { string tableName = cboTableNames.SelectedValue.ToString(); if (tableName == "Products") { int selectedID = (int)cboId.SelectedValue; lblName.Text = "Product Name"; try { currentProd = ProductsDB.GetProductById(selectedID); DisplayCurrentProductData(); } catch (Exception ex) { MessageBox.Show("Error while retrieving product with selected ID: " + ex.Message, ex.GetType().ToString()); } } else if (tableName == "Suppliers") { int selectedID = (int)cboId.SelectedValue; lblName.Text = "Supplier Name"; try { currentSupplier = SuppliersDB.GetSupplierById(selectedID); DisplayCurrentSupplierData(); } catch (Exception ex) { MessageBox.Show("Error while retrieving supplier with selected ID: " + ex.Message, ex.GetType().ToString()); } } else if (tableName == "Products_Suppliers") { int selectedID = (int)cboId.SelectedValue; try { currentProductSupplier = Products_SuppliersDB.GetProductSupplierById(selectedID); DisplayCurrentProductSupplierData(); } catch (Exception ex) { MessageBox.Show("Error while retrieving product supplier with selected ID: " + ex.Message, ex.GetType().ToString()); } } else if (tableName == "Packages_Products_Suppliers") { int selectedID = (int)cboId.SelectedValue; try { productSupplierIds = Packages_Products_SuppliersDB.GetProductSupplierIds(selectedID); DisplayCurrentPackageProductSupplierData(); } catch (Exception ex) { MessageBox.Show("Error while retrieving package product supplier with selected ID: " + ex.Message, ex.GetType().ToString()); } } else if (tableName == "Packages") { int selectedID = (int)cboId.SelectedValue; lblName.Text = "Package Name"; try { productSupplierIds = Packages_Products_SuppliersDB.GetProductSupplierIds(selectedID); DisplayCurrentPackageProductSupplierData(); } catch (Exception ex) { MessageBox.Show("Error while retrieving package product supplier with selected ID: " + ex.Message, ex.GetType().ToString()); } try { currentPackage = PackagesDB.GetPackageById(selectedID); txtName.Text = currentPackage.PkgName; } catch (Exception ex) { MessageBox.Show("Error while retrieving package with selected ID: " + ex.Message, ex.GetType().ToString()); } } }