Exemplo n.º 1
0
        ////GET PRODUCT BY ID ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///Retrieves product by ID
        public ProductsModel GetProductByID()
        {
            ProductsModel product = new ProductsModel();

            Console.WriteLine("       +----------------------------------------------------------------+");
            Console.WriteLine("         Enter your Product ID Number ");
            Console.WriteLine("       +----------------------------------------------------------------+");
            long productID = Convert.ToInt32(Console.ReadLine());

            string sqlQuery = "Select * from products where productID=@ID";

            //Using SqlConnection to establish connection to database
            //using SqlCommand passing in the SqlConnection and the sqlQuery
            using (SqlConnection con = new SqlConnection(CONNECTION))
                using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
                {
                    con.Open();
                    cmd.Parameters.Add("@ID", SqlDbType.Int).Value = productID;

                    //SqlDataReader is used because we are reading data from the database
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            product.productID         = Convert.ToInt32(reader["productID"]);
                            product.productName       = reader["productName"].ToString();
                            product.productPrice      = Convert.ToDouble(reader["productPrice"]);
                            product.productQuantity   = Convert.ToInt32(reader["productQuantity"]);
                            product.productLocationID = Convert.ToInt32(reader["productLocationID"]);

                            Console.WriteLine("Add Product quantity");
                            int amount = Convert.ToInt32(Console.ReadLine());

                            product.Addstock(amount);
                        }

                        {
                            Console.WriteLine();
                            Console.WriteLine("       +================================================================+");
                            Console.WriteLine("       +----------------------------------------------------------------+");
                            Console.WriteLine("        PRODUCT ID       " + " | " + productID);
                            Console.WriteLine("        PRODUCT NAME     " + " | " + product.productName);
                            Console.WriteLine("        PRODUCT PRICE    " + " | " + product.productPrice);
                            Console.WriteLine("        PRODUCT QUANTITY " + " | " + product.productQuantity);
                            Console.WriteLine("        PRODUCT LOCATION " + " | " + product.productLocationID);

                            Console.WriteLine("       +----------------------------------------------------------------+");
                            Console.WriteLine();
                        }
                    }
                    return(product);
                }
        }