/// <summary> /// FrmAddProduct() /// Form Constructor /// </summary> /// <param name="businessArg"></param> public FrmAddProduct(Business businessArg) { InitializeComponent(); business = businessArg; categories = business.Categories; suppliers = business.Suppliers; newProduct = new Product();//represents product to be added to database updatedProduct = new Product();//represents product to be updated in database }
/// <summary> /// productReturned() /// An event handler used to handle events called "eventProductChosen" on FrmCatalog /// If product is returned, it captures its properties to create an order detail object /// Order detail object then added to a datagridview control /// </summary> /// <param name="product">Object of Product></param> private void productReturned(Product product) { this.Enabled = true;//enable this form if (product == null)//return if a product is not selected return; //add order detail to grid OrderDetail orderDetail; const Int16 DEFAULT_QUANTITY = 1; foreach (DataGridViewRow currentRow in gridViewOrderDetails.Rows)//loop through gridlist { orderDetail = (OrderDetail)currentRow.Tag; //if product exists in datagridview; if (orderDetail.ProductID == product.ProductID) { orderDetail.Quantity += 1;// increase its quantity by 1 currentRow.Cells[colQuantity.Name].Value = orderDetail.Quantity.ToString();//display new value in grid cell return;//exit method } } //if product does not exist in datagridview already, create new order detail object and set its properties orderDetail = new OrderDetail(); orderDetail.ProductName = product.ProductName; orderDetail.ProductID = product.ProductID; orderDetail.Quantity = DEFAULT_QUANTITY; orderDetail.UnitPrice = product.UnitPrice; int rowNum = gridViewOrderDetails.Rows.Add(1);//adds a new row and return its index DataGridViewRow row = gridViewOrderDetails.Rows[rowNum];//sets row equal to newly created row //populate cells with order details and product name row.Cells[colProduct.Name].Value = orderDetail.ProductName; row.Cells[colProductID.Name].Value = orderDetail.ProductID; row.Cells[colUnitPrice.Name].Value = orderDetail.UnitPrice.ToString("F"); row.Cells[colQuantity.Name].Value = 1; row.Tag = orderDetail; //set first quantity item control for edit gridViewOrderDetails.CurrentCell = gridViewOrderDetails.Rows[0].Cells[colQuantity.Name]; gridViewOrderDetails.BeginEdit(true); }
//add product to DB and call delegate /// <summary> /// UpdateProduct(Product prod) /// Update product passed to Products table based on the product ID /// </summary> /// <param name="prod"></param> /// <returns></returns> public string UpdateProduct(Product prod) { String updateQuery = "UPDATE Products " + "SET ProductName = " + ((String.IsNullOrEmpty(prod.ProductName)) ? "null" : "\"" + prod.ProductName + "\"") + ", SupplierID = " + ((prod.SupplierID == 0) ? "null" : prod.SupplierID.ToString()) + ", CategoryID = " + ((prod.CategoryID == 0) ? "null" : prod.CategoryID.ToString()) + ", QuantityPerUnit = " + ((String.IsNullOrEmpty(prod.QuantityPerUnit)) ? "null" : "'" + prod.QuantityPerUnit + "'") + ", UnitPrice = " + prod.UnitPrice + ", UnitsInStock = " + prod.UnitsInStock + ", UnitsOnOrder = " + prod.UnitsOnOrder + ", ReorderLevel = " + prod.ReorderLevel + ", Discontinued = " + prod.Discontinued + " WHERE ProductID = " + prod.ProductID; OleDbCommand cmUpdate = new OleDbCommand(updateQuery, con); try { con.Open(); cmUpdate.ExecuteNonQuery(); } catch (Exception) { //can't display any error feedback in data access layer return "Product Updated Failed"; } finally { con.Close(); } //raise event if (productAdded_Changed != null)//check to makes sure at least one handler registered { productAdded_Changed(); } return "Product Updated"; }
//add product to DB and call delegate /// <summary> /// InsertProduct() /// Takes a product object and adds a new record to the database table Products /// </summary> /// <param name="p">Object of type Product</param> public string InsertProduct(Product prod) { String insertQuery = "INSERT INTO Products (ProductName, SupplierID, CategoryID, " + "QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued) VALUES (" + ((String.IsNullOrEmpty(prod.ProductName)) ? "null" : "\"" + prod.ProductName + "\"") + "," + ((prod.SupplierID == 0) ? "null" : prod.SupplierID.ToString()) + "," + ((prod.CategoryID == 0) ? "null" : prod.CategoryID.ToString()) + "," + ((String.IsNullOrEmpty(prod.QuantityPerUnit)) ? "null" : "'" + prod.QuantityPerUnit + "'") + "," + prod.UnitPrice + "," + prod.UnitsInStock + "," + prod.UnitsOnOrder + "," + prod.ReorderLevel + "," + prod.Discontinued + ")"; OleDbCommand cmInsert = new OleDbCommand(insertQuery, con); try { con.Open(); cmInsert.ExecuteNonQuery(); } catch (Exception) { //can't display any error feedback in data access layer return "Product Added Failed"; } finally { con.Close(); } //raise event if (productAdded_Changed != null)//check to makes sure at least one handler registered { productAdded_Changed(); } return "Product Added"; }
/// <summary> /// productChosen() /// Handles "eventProductChosen" on FrmCatalog /// Gets a product from delegate and fills the Update form with its property values. /// <param name="Product">Object of type Product></param> private void productChosen(Product product) { Enabled = true;//enable this form selectedProduct = product; if (selectedProduct == null)//return if there aren't any selected products return; errorProvider.SetError(btnCatalog, ""); foreach (Category c in categories) { if (selectedProduct.CategoryID == c.CategoryID) cmbUpdateCategory.SelectedItem = c; } foreach (Supplier s in suppliers) { if (selectedProduct.CompanyName == s.CompanyName) cmbUpdateSupplier.SelectedItem = s; } toggleInput(true); tbxUpdateProductName.Text = selectedProduct.ProductName; tbxUpdateQtyPerUnit.Text = selectedProduct.QuantityPerUnit; tbxUpdateUnitPrice.Text = selectedProduct.UnitPrice.ToString("F"); tbxUpdateUnitsInStock.Text = selectedProduct.UnitsInStock.ToString(); tbxUpdateUnitsOnOrder.Text = selectedProduct.UnitsOnOrder.ToString(); tbxUpdateReorderLevel.Text = selectedProduct.ReorderLevel.ToString(); chkBoxDiscontinued.Checked = selectedProduct.Discontinued; }
/// <summary> /// updateProduct() /// Updates a record in Products Table /// </summary> /// <param name="_product">takes and object of type Product</param> public string updateProduct(Product _product) { string confirmation = database.UpdateProduct(_product); return confirmation; }
/// <summary> /// fetchProducts() /// Gets product records from database and returns a copy of it /// </summary> /// <returns>List of type Product</returns> public void loadProductData() { products = database.FetchProducts(); List<Product> copyProducts = new List<Product>(); Product copy; foreach (Product p in products) { copy = new Product();//create new object //add properties copy.ProductID = p.ProductID; copy.ProductName = p.ProductName; copy.CategoryID = p.CategoryID; copy.QuantityPerUnit = p.QuantityPerUnit; copy.UnitPrice = p.UnitPrice; copy.UnitsInStock = p.UnitsInStock; copy.UnitsOnOrder = p.UnitsOnOrder; copy.ReorderLevel = p.ReorderLevel; copy.Discontinued = p.Discontinued; copy.CompanyName = p.CompanyName; copy.Picture = p.Picture; copyProducts.Add(copy);//add to new list products = copyProducts; } }
/// <summary> /// fetchProductByID /// Takes productID as parameter and returns a Product Object /// </summary> /// <param name="productId">of Type int</param> /// <returns>Product Object</returns> public Product fetchProductByID(int productId) { Product copy; foreach (Product product in products) { if (product.ProductID == productId) { copy = new Product();//create new object copy = product; return copy; } } return null; }
/// <summary> /// addProduct() /// Adds new record to Products Table /// </summary> /// <param name="_product">takes and object of type Product</param> public string addProduct(Product _product) { string confirmation = database.InsertProduct(_product); return confirmation; }
/// <summary> /// btn_OK_Click() /// Get the product object for each selected item in order to send back to Order form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_OK_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in gridProducts.SelectedRows)//loop through selected items in gridProducts window { selectedProduct = (Product)row.Tag; //for each item, capture product associated with the tag } Close(); }