/// <summary> /// Update Products based on selected supplier. /// </summary> public void UpdateProducts(ProductsDO products) { string currentMethod = "UpdateProducts"; //Opening SQL connection. try { using (SqlConnection northWndConn = new SqlConnection(connectionString)) { //Creating a new SqlCommand to use a stored procedure. SqlCommand enterCommand = new SqlCommand("UPDATE_PRODUCTS", northWndConn); //Parameters that are being passed to the stored procedures. enterCommand.CommandType = CommandType.StoredProcedure; enterCommand.Parameters.AddWithValue("@ProductId", products.ProductId); enterCommand.Parameters.AddWithValue("@ProductName", products.ProductName); enterCommand.Parameters.AddWithValue("@QuantityPerUnit", products.QuantityPerUnit); enterCommand.Parameters.AddWithValue("@UnitPrice", products.UnitPrice); enterCommand.Parameters.AddWithValue("@UnitsInStock", products.UnitsInStock); enterCommand.Parameters.AddWithValue("@UnitsOnOrder", products.UnitsOnOrder); enterCommand.Parameters.AddWithValue("@ReorderLevel", products.ReorderLevel); //Opening connection. northWndConn.Open(); //Execute Non Query. enterCommand.ExecuteNonQuery(); } } catch (SqlException ex) { //Prints error to console and logs. ProductsErrorHandler(ex, currentClass, currentMethod, ex.StackTrace); throw ex; } }
/// <summary> /// Maps all products from the datarow and returns the ProductsDO object. /// </summary> /// <param name="dataRow"></param> /// <returns></returns> public ProductsDO MapAllProducts(DataRow dataRow) { string currentMethod = "MapAllProducts"; try { ProductsDO products = new ProductsDO(); //If the Product Id is not null then add values to the product object from the database. if (dataRow["ProductID"] != DBNull.Value) { products.ProductId = (int)dataRow["ProductID"]; } products.ProductName = dataRow["ProductName"].ToString(); products.QuantityPerUnit = dataRow["QuantityPerUnit"].ToString(); products.UnitPrice = (decimal)dataRow["UnitPrice"]; products.UnitsInStock = (Int16)dataRow["UnitsInStock"]; products.UnitsOnOrder = (Int16)dataRow["UnitsOnOrder"]; products.ReorderLevel = (Int16)dataRow["ReorderLevel"]; //Returning the object with a row updated from SQL. return(products); } catch (Exception ex) { //Prints error to console and logs. ProductsErrorHandler(ex, currentClass, currentMethod, ex.StackTrace); throw ex; } }
/// <summary> /// View Products from selected supplier. /// </summary> public List <ProductsDO> ViewProducts(ProductsDO products) { string currentMethod = "ViewProducts"; try { //Instatiating List <ProductsDO> allProducts = new List <ProductsDO>(); //Opening SQL connection. using (SqlConnection northWndConn = new SqlConnection(connectionString)) { //Creating a new SqlCommand to use a stored procedure. SqlCommand enterCommand = new SqlCommand("DISPLAY_PRODUCTS", northWndConn); enterCommand.CommandType = CommandType.StoredProcedure; enterCommand.Parameters.AddWithValue("@SupplierId", products.SupplierId); northWndConn.Open(); //Using SqlDataAdapter to get SQL table. DataTable ProductInfo = new DataTable(); using (SqlDataAdapter productAdapter = new SqlDataAdapter(enterCommand)) { productAdapter.Fill(ProductInfo); productAdapter.Dispose(); } //Putting datarow into a List of the products object. foreach (DataRow row in ProductInfo.Rows) { ProductsDO mappedRow = MapAllProducts(row); allProducts.Add(mappedRow); } } //Returning an updated list of the products object. return(allProducts); } catch (Exception ex) { //Prints error to console and logs. ProductsErrorHandler(ex, currentClass, currentMethod, ex.StackTrace); throw ex; } }
/// <summary> /// Gets User input for the products menu. /// </summary> /// <returns></returns> private static ProductsDO GetUserInputForProducts() { string currentMethod = "GetUserInputForProducts"; do { try { ProductsDO products = new ProductsDO(); //Collect information from the user. Console.WriteLine("Please enter product name."); products.ProductName = Console.ReadLine(); Console.WriteLine("Please enter quantity per unit."); products.QuantityPerUnit = Console.ReadLine(); Console.WriteLine("Please enter unit price."); decimal.TryParse(Console.ReadLine(), out decimal unitPrice); Console.WriteLine("Please enter units in stock."); int.TryParse(Console.ReadLine(), out int unitsInStock); Console.WriteLine("Please enter units on order."); int.TryParse(Console.ReadLine(), out int unitsOnOrder); Console.WriteLine("Please enter reorder level."); int.TryParse(Console.ReadLine(), out int reorderLevel); products.UnitsInStock = unitsInStock; products.UnitPrice = unitPrice; products.UnitsOnOrder = unitsOnOrder; products.ReorderLevel = reorderLevel; //returning object back to who called it. return(products); } catch (Exception ex) { string currentClass = "Program"; ProductsDAO productsDAO = new ProductsDAO(); //Prints error to console, logs, and restarts the menu. productsDAO.ProductsErrorHandler(ex, currentClass, currentMethod, ex.StackTrace); throw ex; } } while (true); }
/// <summary> /// Takes user to a products menu. /// </summary> static void ProductsMenu() { string currentMethod = "ProductsMenu"; //Instantiating objects. ProductsDAO productsDAO = new ProductsDAO(); //Add display method for products here------------------------------------------------------------------ do { try { ProductsDO products = new ProductsDO(); //Menu Options. Console.Clear(); Console.WriteLine("\t\tPlease select option by pressing a key:\n"); Console.WriteLine(" 1) Display all products"); Console.WriteLine(" 2) Update existing products"); Console.WriteLine(" 3) Return to Supplier menu"); Console.WriteLine(" 4) Exit Application"); //Allows user to press either number keys on keyboard or use num pad. ConsoleKeyInfo keyPressed = Console.ReadKey(); Console.Clear(); //Determines which key was pressed. switch (keyPressed.Key) { //Display--------------------------------------------------------------------- case ConsoleKey.NumPad1: case ConsoleKey.D1: //Creates a new list object called items to store our list object returned from ViewProducts(). Console.WriteLine("Which Supplier Id would you like to view products for?"); int.TryParse(Console.ReadLine(), out int supplierId); products.SupplierId = supplierId; //Getting a list of our object returned from ViewProducts() List <ProductsDO> items = productsDAO.ViewProducts(products); DisplayProducts(items); break; //Update---------------------------------------------------------------------- case ConsoleKey.NumPad2: case ConsoleKey.D2: Console.WriteLine("Please enter product Id for the row you would like to change."); //Getting the product Id that user wants to update. int.TryParse(Console.ReadLine(), out int productId); //Adding information returned from GetUserInputForProducts() into our object. products = GetUserInputForProducts(); //Setting the value of our object variable for productId. products.ProductId = productId; //Passing all the user input into UpdateProducts() using our object named products. productsDAO.UpdateProducts(products); break; //Return to menu()---------------------------------------------------------------------- case ConsoleKey.NumPad3: case ConsoleKey.D3: Menu(); break; //Exit--------------------------------------------------------------------------------- case ConsoleKey.NumPad4: case ConsoleKey.D4: Environment.Exit(0); break; default: break; } } catch (Exception ex) { string currentClass = "Program"; //Prints error to console, logs, and restarts the menu. productsDAO.ProductsErrorHandler(ex, currentClass, currentMethod, ex.StackTrace); throw ex; } } while (true); }