private void dataGridProdSup_SelectionChanged(object sender, EventArgs e) { if (dataGridProdSup.SelectedRows.Count > 0) { currentProductSupplier = (ProductsSupplier)dataGridProdSup.SelectedRows[0].DataBoundItem; } }
// Update Products Supplier Table public static bool UpdateProductsSupplier(ProductsSupplier oldProdSuppObj, ProductsSupplier newProdSuppObj) { bool updateSuccess = true; string updateStatement = "UPDATE Products_Suppliers " + "SET ProductId = @newProductId, " + "SupplierId = @newSupplierId " + "WHERE ProductSupplierId = @oldProductSupplierId " + "AND (ProductId = @oldProductId " + "OR ProductId IS NULL AND @oldProductId IS NULL) " + "AND (SupplierId = @oldSupplierId " + "OR SupplierId IS NULL AND @oldSupplierId IS NULL)"; // Get connection to Travel Experts DB SqlConnection connection = TravelExpertsDB.GetConnection(); // Create a select command object SqlCommand updateCmd = new SqlCommand(updateStatement, connection); // Assign value to parameter(s) // Verify if newProdSuppObj.ProductId is null if (newProdSuppObj.ProductId == null) { updateCmd.Parameters.AddWithValue("@newProductId", DBNull.Value); } else { updateCmd.Parameters.AddWithValue("@newProductId", newProdSuppObj.ProductId); } // Verify if newProdSuppObj.SupplierId is null if (newProdSuppObj.SupplierId == null) { updateCmd.Parameters.AddWithValue("@newSupplierId", DBNull.Value); } else { updateCmd.Parameters.AddWithValue("@newSupplierId", newProdSuppObj.SupplierId); } // Execute the update command try { connection.Open(); int rowsUpdated = updateCmd.ExecuteNonQuery(); // Check for concurrency, another user might have updated or deleted in the meantime if (rowsUpdated == 0) { updateSuccess = false; } } catch (Exception ex) { throw ex; } finally { connection.Close(); } return(updateSuccess); }
// Get Product Suppliers by product id and supplier id public static ProductsSupplier GetProductsSupplierByProductIdAndSupplierId(int productId, int supplierId) { ProductsSupplier prodSuppObj = null; string selectStatement = "SELECT ProductSupplierId, ProductId, SupplierId " + "FROM Products_Suppliers " + "WHERE ProductId = @productId " + "AND SupplierId = @supplierId"; // Get connection to Travel Experts DB SqlConnection connection = TravelExpertsDB.GetConnection(); // Create a select command object SqlCommand selectCmd = new SqlCommand(selectStatement, connection); // Assign value to parameter(s) selectCmd.Parameters.AddWithValue("@productId", productId); selectCmd.Parameters.AddWithValue("@supplierId", supplierId); // Execute the select command and start the reading process from DB try { connection.Open(); SqlDataReader dr = selectCmd.ExecuteReader(); if (dr.Read()) // if exists { prodSuppObj = new ProductsSupplier(); prodSuppObj.ProductSupplierId = (int)dr["ProductSupplierId"]; // Both Product ID and Supplier ID can be null, need to verify while reading // from DB if (dr["ProductId"] is DBNull) { prodSuppObj.ProductId = null; } else { prodSuppObj.ProductId = (int)dr["ProductId"]; } if (dr["SupplierId"] is DBNull) { prodSuppObj.SupplierId = null; } else { prodSuppObj.SupplierId = (int)dr["SupplierId"]; } } } catch (Exception ex) { throw ex; } finally { connection.Close(); } return(prodSuppObj); }
// Get All products suppliers from the DB public static List <ProductsSupplier> GetAllProductsSuppliers() { List <ProductsSupplier> prodSuppList = new List <ProductsSupplier>(); string selectStatement = "SELECT ProductSupplierId, ProductId, SupplierId " + "FROM Products_Suppliers " + "ORDER BY ProductSupplierId"; // Get connection to Travel Experts DB SqlConnection connection = TravelExpertsDB.GetConnection(); // Create a select command object SqlCommand selectCmd = new SqlCommand(selectStatement, connection); // Execute the select command and start the reading process from DB try { connection.Open(); SqlDataReader dr = selectCmd.ExecuteReader(); while (dr.Read()) { ProductsSupplier prodSuppObj = new ProductsSupplier(); prodSuppObj.ProductSupplierId = (int)dr["ProductSupplierId"]; // Both Product ID and Supplier ID can be null, need to verify while reading // from DB if (dr["ProductId"] is DBNull) { prodSuppObj.ProductId = null; } else { prodSuppObj.ProductId = (int)dr["ProductId"]; } if (dr["SupplierId"] is DBNull) { prodSuppObj.SupplierId = null; } else { prodSuppObj.SupplierId = (int)dr["SupplierId"]; } // Add to product suppliers list prodSuppList.Add(prodSuppObj); } } catch (Exception ex) { throw ex; } finally { connection.Close(); } return(prodSuppList); }
private void productSupplierIdComboBox_SelectedIndexChanged(object sender, EventArgs e) { try { // Get the current selected Product Supplier ID int selectedProdSuppId = (int)productSupplierIdComboBox.SelectedValue; // Get the Product Supplier Object from the list of the selected Product Supplier ID selectedProdSuppObj = (from prodSupp in prodSuppList where prodSupp.ProductSupplierId == selectedProdSuppId select prodSupp).Single(); DisplayProdSupp(selectedProdSuppObj); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message, ex.GetType().ToString()); } }
// Insert a new product public static int AddProductsSupplier(ProductsSupplier prodSuppObj) { int prodSuppId = 0; string insertStatement = "INSERT INTO Products_Suppliers (ProductId, SupplierId) " + "OUTPUT Inserted.ProductSupplierId " + "VALUES (@ProductId, @SupplierId)"; // Get connection to Travel Experts DB SqlConnection connection = TravelExpertsDB.GetConnection(); // Create an insert command object SqlCommand insertCmd = new SqlCommand(insertStatement, connection); // Assign value to parameter(s) // Verify if Product ID from object is null if (prodSuppObj.ProductId == null) { insertCmd.Parameters.AddWithValue("@ProductId", DBNull.Value); } else { insertCmd.Parameters.AddWithValue("@ProductId", prodSuppObj.ProductId); } // Verify if Supplier ID from object is null if (prodSuppObj.SupplierId == null) { insertCmd.Parameters.AddWithValue("@SupplierId", DBNull.Value); } else { insertCmd.Parameters.AddWithValue("@SupplierId", prodSuppObj.SupplierId); } // Execute the insert command try { connection.Open(); // Returns the auto generated ProductSupplierId prodSuppId = (int)insertCmd.ExecuteScalar(); } catch (Exception ex) { throw ex; } finally { connection.Close(); } return(prodSuppId); }
// Display Product Supplier Information private void DisplayProdSupp(ProductsSupplier prodSuppObj) { if (prodSuppObj.ProductId == null) { productIdTextBox.Text = ""; } else { productIdTextBox.Text = prodSuppObj.ProductId.ToString(); } if (prodSuppObj.SupplierId == null) { supplierIdTextBox.Text = ""; } else { supplierIdTextBox.Text = prodSuppObj.SupplierId.ToString(); } }
private void saveBtn_Click(object sender, EventArgs e) { productsupplier = new ProductsSupplier(); productsupplier.ProductSupplierId = Convert.ToInt32(ProdSupIDTxt.Text); productsupplier.ProductId = Convert.ToInt32(ProductIDTxt.Text); productsupplier.SupplierId = Convert.ToInt32(SupplierIdTxt.Text); if (AddButton == true) { context.ProductsSuppliers.Add(productsupplier); } else { context.ProductsSuppliers.Update(productsupplier); } context.SaveChanges(); MessageBox.Show("Record inserted succesfully"); this.Close(); }
/// <summary> /// Get ProductsSupplier record for specified ProductsSupplier ID /// </summary> /// <param name="productSupplierId">ProductsSupplier ID</param> /// <returns>ProductsSupplier Object</returns> public static ProductsSupplier GetProductSupplier(int productsSupplierId) { ProductsSupplier productsupplier = new ProductsSupplier(); SqlConnection connection = TravelExpertsDB.GetConnection(); string query = "SELECT ProductSupplierId, ps.ProductId, ps.SupplierId, ProdName, SupName " + " FROM Products_Suppliers ps " + " INNER JOIN Products p ON ps.ProductId = p.ProductId " + " INNER JOIN Suppliers s ON ps.SupplierId = s.SupplierId " + " WHERE ProductSupplierId = @ProductSupplierId "; SqlCommand command = new SqlCommand(query, connection); command.Parameters.AddWithValue("@ProductSupplierId", productsSupplierId); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow); if (reader.Read()) { productsupplier.ProductSupplierId = (int)reader["ProductSupplierId"]; productsupplier.ProductId = (int)reader["ProductId"]; productsupplier.SupplierId = (int)reader["SupplierId"]; productsupplier.ProdName = reader["ProdName"].ToString(); productsupplier.SupName = reader["SupName"].ToString(); } else { return null; } } catch (SqlException ex) { throw ex; } finally { connection.Close(); } return productsupplier; }
// Adding a new Products Supplier private void btnAddProdSupp_Click(object sender, EventArgs e) { ProductsSupplier newProdSuppObj = new ProductsSupplier(); // if productIdTextBoxAdd is empty if (string.IsNullOrWhiteSpace(productIdTextBoxAdd.Text)) { newProdSuppObj.ProductId = null; } else { newProdSuppObj.ProductId = Convert.ToInt32(productIdTextBoxAdd.Text); } // if supplierIdTextBoxAdd is empty if (string.IsNullOrWhiteSpace(supplierIdTextBoxAdd.Text)) { newProdSuppObj.SupplierId = null; } else { newProdSuppObj.SupplierId = Convert.ToInt32(supplierIdTextBoxAdd.Text); } try { newProdSuppObj.ProductSupplierId = ProductsSupplierDB.AddProductsSupplier(newProdSuppObj); // Add to the Product Supplier List prodSuppList.Add(newProdSuppObj); // Reload combo box LoadComboBox(); int searchIndex = productSupplierIdComboBox.Items.IndexOf(newProdSuppObj.ProductSupplierId); productSupplierIdComboBox.SelectedIndex = searchIndex; } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message, ex.GetType().ToString()); } }
/// <summary> /// Get List of all ProductsSupplier records /// </summary> /// <returns>List of ProductsSupplier objects</returns> public static List<ProductsSupplier> GetProductsSuppliers() { List<ProductsSupplier> productsuppliers = new List<ProductsSupplier>(); SqlConnection connection = TravelExpertsDB.GetConnection(); string query = "SELECT ProductSupplierId, ps.ProductId, ps.SupplierId, ProdName, SupName " + " FROM Products_Suppliers ps " + " INNER JOIN Products p ON ps.ProductId = p.ProductId " + " INNER JOIN Suppliers s ON ps.SupplierId = s.SupplierId "; SqlCommand command = new SqlCommand(query, connection); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { ProductsSupplier productsupplier = new ProductsSupplier(); productsupplier.ProductSupplierId = (int)reader["ProductSupplierId"]; productsupplier.ProductId = (int)reader["ProductId"]; productsupplier.SupplierId = (int)reader["SupplierId"]; productsupplier.ProdName = reader["ProdName"].ToString(); productsupplier.SupName = reader["SupName"].ToString(); productsuppliers.Add(productsupplier); } } catch (SqlException ex) { throw ex; } finally { connection.Close(); } return productsuppliers; }
private void frmUpdatePackages_Load(object sender, EventArgs e) { // Create a copy of the selected object; OldPackage = new Package { PackageId = SelectedPackage.PackageId, PkgAgencyCommission = SelectedPackage.PkgAgencyCommission, PkgBasePrice = SelectedPackage.PkgBasePrice, PkgDesc = SelectedPackage.PkgDesc, PkgEndDate = SelectedPackage.PkgEndDate, PkgName = SelectedPackage.PkgName, PkgStartDate = SelectedPackage.PkgStartDate }; OldProductsSupplier = new ProductsSupplier { ProductSupplierId = SelectedProductsSupplier.ProductSupplierId, ProductId = SelectedProductsSupplier.ProductId, SupplierId = SelectedProductsSupplier.SupplierId }; OldPkgProdSupp = new PackagesProductsSuppliers { PackageId = SelectedPkgProdSupp.PackageId, ProductSupplierId = SelectedPkgProdSupp.ProductSupplierId }; // Populate combo boxes prodNameComboBox.DisplayMember = "ProdName"; prodNameComboBox.ValueMember = "ProductId"; prodNameComboBox.DataSource = Products.OrderBy(p => p.ProdName).ToList(); supNameComboBox.DisplayMember = "SupName"; supNameComboBox.ValueMember = "SupplierId"; supNameComboBox.DataSource = Suppliers.OrderBy(s => s.SupName).ToList(); pkgNameTextBox.Text = SelectedPackage.PkgName; pkgBasePriceTextBox.Text = SelectedPackage.PkgBasePrice.ToString(); if (SelectedPackage.PkgDesc == null) { pkgDescTextBox.Text = string.Empty; } else { pkgDescTextBox.Text = SelectedPackage.PkgDesc; } DateTime startDate = SelectedPackage.PkgStartDate ?? DateTime.MinValue; if (startDate == DateTime.MinValue) { pkgStartDateDateTimePicker.Format = DateTimePickerFormat.Custom; pkgStartDateDateTimePicker.CustomFormat = " "; pkgStartDateDateTimePicker.Checked = false; } else { pkgStartDateDateTimePicker.Format = DateTimePickerFormat.Long; pkgStartDateDateTimePicker.Value = startDate; } DateTime endDate = SelectedPackage.PkgEndDate ?? DateTime.MinValue; if (endDate == DateTime.MinValue) { pkgEndDateDateTimePicker.Format = DateTimePickerFormat.Custom; pkgEndDateDateTimePicker.CustomFormat = " "; pkgEndDateDateTimePicker.Checked = false; } else { pkgEndDateDateTimePicker.Format = DateTimePickerFormat.Long; pkgEndDateDateTimePicker.Value = endDate; } pkgAgencyCommissionTextBox.Text = SelectedPackage.PkgAgencyCommission.ToString(); prodNameComboBox.SelectedValue = SelectedProductsSupplier.ProductId; supNameComboBox.SelectedValue = SelectedProductsSupplier.SupplierId; }
private void btnUpdate_Click(object sender, EventArgs e) { string pkgName; decimal basePrice; int productId; int supplierId; if (Validator.IsPresent(pkgNameTextBox)) { pkgName = pkgNameTextBox.Text; // If Package Name already exists, get package object from database bool ValidPrice = true; bool DateValid = true; if (Validator.IsPresent(pkgBasePriceTextBox)) { basePrice = Convert.ToDecimal(pkgBasePriceTextBox.Text); SelectedPackage.PkgBasePrice = basePrice; SelectedPackage.PkgName = pkgName; if (string.IsNullOrWhiteSpace(pkgAgencyCommissionTextBox.Text)) { SelectedPackage.PkgAgencyCommission = null; } else { SelectedPackage.PkgAgencyCommission = Convert.ToDecimal(pkgAgencyCommissionTextBox.Text); } if (string.IsNullOrWhiteSpace(pkgDescTextBox.Text)) { SelectedPackage.PkgDesc = null; } else { SelectedPackage.PkgDesc = pkgDescTextBox.Text; } if (pkgStartDateDateTimePicker.Checked == false && pkgEndDateDateTimePicker.Checked == false) { SelectedPackage.PkgStartDate = null; SelectedPackage.PkgEndDate = null; } else if (pkgStartDateDateTimePicker.Checked == true && pkgEndDateDateTimePicker.Checked == false) { // Package End Date has not been provided yet SelectedPackage.PkgStartDate = pkgStartDateDateTimePicker.Value.Date; SelectedPackage.PkgEndDate = null; } else if (pkgStartDateDateTimePicker.Checked == false && pkgEndDateDateTimePicker.Checked == true) { MessageBox.Show("Must Provide a Valid Start Date"); DateValid = false; DialogResult = DialogResult.None; } else { if (DateTime.Compare(pkgStartDateDateTimePicker.Value, pkgEndDateDateTimePicker.Value) >= 0) { MessageBox.Show("Start Date must be earlier than End Date"); DateValid = false; DialogResult = DialogResult.None; } else { SelectedPackage.PkgStartDate = pkgStartDateDateTimePicker.Value.Date; SelectedPackage.PkgEndDate = pkgEndDateDateTimePicker.Value.Date; } } if (DateValid) { // Update package in database try { if (!PackageDB.UpdatePackage(OldPackage, SelectedPackage)) { MessageBox.Show("Another user has updated or " + "deleted that Package.", "Database Error"); DialogResult = DialogResult.Retry; PkgUpdated = false; } else { PkgUpdated = true; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } } else { ValidPrice = false; DialogResult = DialogResult.None; } if (ValidPrice && DateValid && PkgUpdated) { productId = (int)prodNameComboBox.SelectedValue; supplierId = (int)supNameComboBox.SelectedValue; // Check if the combination of productId and supplierId already exists in database if (ProductsSupplierDB.GetProductsSupplierByProductIdAndSupplierId(productId, supplierId) == null) { // if doesn't exist in database, insert a new record ProductsSupplier prodSupps = new ProductsSupplier { ProductId = productId, SupplierId = supplierId }; // Insert into database int prodSuppId = ProductsSupplierDB.AddProductsSupplier(prodSupps); prodSupps.ProductSupplierId = prodSuppId; SelectedProductsSupplier = prodSupps; ProdSuppInserted = true; } else { SelectedProductsSupplier = ProductsSupplierDB.GetProductsSupplierByProductIdAndSupplierId(productId, supplierId); ProdSuppInserted = false; } // Update Packages_Products_Supplier table try { // If there already exists a same packageId and ProductSupplierId combination var pkgProdSuppTable = PkgProdSupps.SingleOrDefault(p => p.PackageId == SelectedPackage.PackageId && p.ProductSupplierId == SelectedProductsSupplier.ProductSupplierId); if (pkgProdSuppTable != null) { MessageBox.Show("Package: " + SelectedPackage.PkgName + " with \n" + "Product Name: " + prodNameComboBox.Text + "\nSupplier Name: " + supNameComboBox.Text + " \nalready exists", "Record Exists"); DialogResult = DialogResult.None; } else { // Verify against database PackagesProductsSuppliers pkgPS = PackagesProductsSuppliersDB.GetPackagesProductsSuppliersByPkgIdAndProductSupplierId(SelectedPackage.PackageId, SelectedProductsSupplier.ProductSupplierId); if (pkgPS == null) { SelectedPkgProdSupp.ProductSupplierId = SelectedProductsSupplier.ProductSupplierId; if (!PackagesProductsSuppliersDB.UpdatePackagesProductsSuppliers(OldPkgProdSupp, SelectedPkgProdSupp)) { MessageBox.Show("Another user has updated or " + "deleted that Package.", "Database Error"); DialogResult = DialogResult.Retry; } else { DialogResult = DialogResult.OK; } } else { MessageBox.Show("Package: " + SelectedPackage.PkgName + " with \n" + "Product Name: " + prodNameComboBox.Text + "\nSupplier Name: " + supNameComboBox.Text + " \nalready exists", "Record Exists"); SelectedPkgProdSupp = pkgPS; DialogResult = DialogResult.OK; } } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } else { DialogResult = DialogResult.None; } } else { DialogResult = DialogResult.None; } }
private void btnAdd_Click(object sender, EventArgs e) { string pkgName; decimal basePrice; int productId; int supplierId; if (Validator.IsPresent(pkgNameTextBox)) { pkgName = pkgNameTextBox.Text; // If Package Name already exists, get package object from database bool ValidPrice = true; bool DateValid = true; var pkg = packages.SingleOrDefault(p => p.PkgName.ToLower() == pkgName.ToLower()); if (pkg != null) { Package = pkg; PkgInserted = false; } else { // Verify against database Package pkgExist = PackageDB.GetPackageByName(pkgName); if (pkgExist == null) { Package newPkg = new Package(); if (Validator.IsPresent(pkgBasePriceTextBox)) { basePrice = Convert.ToDecimal(pkgBasePriceTextBox.Text); newPkg.PkgBasePrice = basePrice; newPkg.PkgName = pkgName; if (string.IsNullOrWhiteSpace(pkgAgencyCommissionTextBox.Text)) { newPkg.PkgAgencyCommission = null; } else { newPkg.PkgAgencyCommission = Convert.ToDecimal(pkgAgencyCommissionTextBox.Text); } if (string.IsNullOrWhiteSpace(pkgDescTextBox.Text)) { newPkg.PkgDesc = null; } else { newPkg.PkgDesc = pkgDescTextBox.Text; } if (pkgStartDateDateTimePicker.Checked == false && pkgEndDateDateTimePicker.Checked == false) { newPkg.PkgStartDate = null; newPkg.PkgEndDate = null; } else if (pkgStartDateDateTimePicker.Checked == true && pkgEndDateDateTimePicker.Checked == false) { // Package End Date has not been provided yet newPkg.PkgStartDate = pkgStartDateDateTimePicker.Value.Date; newPkg.PkgEndDate = null; } else if (pkgStartDateDateTimePicker.Checked == false && pkgEndDateDateTimePicker.Checked == true) { MessageBox.Show("Must Provide a Valid Start Date"); DateValid = false; DialogResult = DialogResult.None; } else { if (DateTime.Compare(pkgStartDateDateTimePicker.Value, pkgEndDateDateTimePicker.Value) >= 0) { MessageBox.Show("Start Date must be earlier than End Date"); DateValid = false; DialogResult = DialogResult.None; } else { newPkg.PkgStartDate = pkgStartDateDateTimePicker.Value.Date; newPkg.PkgEndDate = pkgEndDateDateTimePicker.Value.Date; } } if (DateValid) { // Add package to database int pkgId = PackageDB.AddPackage(newPkg); newPkg.PackageId = pkgId; Package = newPkg; PkgInserted = true; } } else { ValidPrice = false; DialogResult = DialogResult.None; } } else // Package with pkgName exists in database { Package = pkgExist; PkgInserted = true; } } if (ValidPrice && DateValid) { productId = (int)prodNameComboBox.SelectedValue; supplierId = (int)supNameComboBox.SelectedValue; // Check if the combination of productId and supplierId already exists in database if (ProductsSupplierDB.GetProductsSupplierByProductIdAndSupplierId(productId, supplierId) == null) { // if doesn't exist in database, insert a new record ProductsSupplier prodSupps = new ProductsSupplier { ProductId = productId, SupplierId = supplierId }; // Insert into database int prodSuppId = ProductsSupplierDB.AddProductsSupplier(prodSupps); prodSupps.ProductSupplierId = prodSuppId; productsSupplier = prodSupps; ProdSuppInserted = true; } else { productsSupplier = ProductsSupplierDB.GetProductsSupplierByProductIdAndSupplierId(productId, supplierId); ProdSuppInserted = false; } // If there already exists a same packageId and ProductSupplierId combination var pkgProdSuppTable = pkgProdSupps.SingleOrDefault(p => p.PackageId == Package.PackageId && p.ProductSupplierId == productsSupplier.ProductSupplierId); if (pkgProdSuppTable != null) { MessageBox.Show("Package: " + Package.PkgName + " with \n" + "Product Name: " + prodNameComboBox.Text + "\nSupplier Name: " + supNameComboBox.Text + " \nalready exists", "Record Exists"); DialogResult = DialogResult.None; } else { // Verify against database PackagesProductsSuppliers pkgPS = PackagesProductsSuppliersDB.GetPackagesProductsSuppliersByPkgIdAndProductSupplierId(Package.PackageId, productsSupplier.ProductSupplierId); if (pkgPS == null) { PackagesProductsSuppliers pps = new PackagesProductsSuppliers(); pps.PackageId = Package.PackageId; pps.ProductSupplierId = productsSupplier.ProductSupplierId; // Insert into database PackagesProductsSuppliersDB.AddPackagesProductsSuppliers(pps); pkgProdSupp = pps; DialogResult = DialogResult.OK; } else { MessageBox.Show("Package: " + Package.PkgName + " with \n" + "Product Name: " + prodNameComboBox.Text + "\nSupplier Name: " + supNameComboBox.Text + " \nalready exists", "Record Exists"); pkgProdSupp = pkgPS; DialogResult = DialogResult.OK; } } } else { DialogResult = DialogResult.None; } } else { DialogResult = DialogResult.None; } }