public static BLProduct RetrieveFromDB(string databaseConnectionString, int id) { SqlConnection sqlConnection = new SqlConnection(databaseConnectionString); SqlCommand sqlGetProduct = new SqlCommand($"SELECT CategoryID, Name, Description, Price, Popularity, StockQuantity, VAT FROM Products WHERE ID = {id}", sqlConnection); SqlDataReader sqlReader = null; try { sqlConnection.Open(); sqlReader = sqlGetProduct.ExecuteReader(); while (sqlReader.Read()) { BLProduct product = new BLProduct(id: id, categoryId: int.Parse(sqlReader["CategoryID"].ToString()), name: sqlReader["Name"].ToString(), description: sqlReader["Description"].ToString(), price: double.Parse(sqlReader["Price"].ToString()), popularity: int.Parse(sqlReader["Popularity"].ToString()), stockQuantity: int.Parse(sqlReader["StockQuantity"].ToString()), VAT: double.Parse(sqlReader["VAT"].ToString())); product.GetDiscountFromDB(databaseConnectionString); product.GetTrailerUrlFromDB(databaseConnectionString); return product; } return null; } finally { if (sqlReader != null) { sqlReader.Close(); sqlReader.Dispose(); } sqlConnection.Close(); sqlConnection.Dispose(); sqlGetProduct.Dispose(); } }
private void LoadProduct(int productId) { product = BLProduct.RetrieveFromDB(connectionString, productId); if (product == null) throw new HttpException(404, $"Product.aspx?productId={productId} doesn't exist."); productImage.Src = $"ImgHandler.ashx?productId={productId}"; productTitle.InnerText = product.Name; if (product.Discount > 0) { originalProductPrice.InnerText = "£" + product.Price; originalProductPrice.Visible = true; double newPrice = product.Price * (1 - product.Discount / 100); productPrice.Visible = false; discountProductPrice.Visible = true; discountProductPrice.InnerText = "£" + newPrice; } else productPrice.InnerText = "£" + product.Price; productDescription.InnerText = product.Description; if (product.TrailerUrl != null) { linkViewTrailer.Visible = true; linkViewTrailer.HRef = product.TrailerUrl; } }
public static List <BLProduct> RetrieveTopNPopularProductsFromDB(string databaseConnectionString, int numberOfProducts) { List <BLProduct> products = new List <BLProduct>(); SqlConnection sqlConnection = new SqlConnection(databaseConnectionString); SqlCommand sqlGetPopularProducts = new SqlCommand($"SELECT TOP {numberOfProducts} ID, CategoryID, Name, Description, Price, Popularity, StockQuantity, VAT FROM Products ORDER BY Popularity DESC", sqlConnection); SqlDataReader sqlReader = null; try { sqlConnection.Open(); sqlReader = sqlGetPopularProducts.ExecuteReader(); while (sqlReader.Read()) { BLProduct product = new BLProduct(id: int.Parse(sqlReader["ID"].ToString()), categoryId: int.Parse(sqlReader["CategoryID"].ToString()), name: sqlReader["Name"].ToString(), description: sqlReader["Description"].ToString(), price: double.Parse(sqlReader["Price"].ToString()), popularity: int.Parse(sqlReader["Popularity"].ToString()), stockQuantity: int.Parse(sqlReader["StockQuantity"].ToString()), VAT: double.Parse(sqlReader["VAT"].ToString())); product.GetDiscountFromDB(databaseConnectionString); product.GetTrailerUrlFromDB(databaseConnectionString); products.Add(product); } return(products); } finally { if (sqlReader != null) { sqlReader.Close(); sqlReader.Dispose(); } sqlConnection.Close(); sqlConnection.Dispose(); sqlGetPopularProducts.Dispose(); } }
public static BLProduct RetrieveFromDB(string databaseConnectionString, int id) { SqlConnection sqlConnection = new SqlConnection(databaseConnectionString); SqlCommand sqlGetProduct = new SqlCommand($"SELECT CategoryID, Name, Description, Price, Popularity, StockQuantity, VAT FROM Products WHERE ID = {id}", sqlConnection); SqlDataReader sqlReader = null; try { sqlConnection.Open(); sqlReader = sqlGetProduct.ExecuteReader(); while (sqlReader.Read()) { BLProduct product = new BLProduct(id: id, categoryId: int.Parse(sqlReader["CategoryID"].ToString()), name: sqlReader["Name"].ToString(), description: sqlReader["Description"].ToString(), price: double.Parse(sqlReader["Price"].ToString()), popularity: int.Parse(sqlReader["Popularity"].ToString()), stockQuantity: int.Parse(sqlReader["StockQuantity"].ToString()), VAT: double.Parse(sqlReader["VAT"].ToString())); product.GetDiscountFromDB(databaseConnectionString); product.GetTrailerUrlFromDB(databaseConnectionString); return(product); } return(null); } finally { if (sqlReader != null) { sqlReader.Close(); sqlReader.Dispose(); } sqlConnection.Close(); sqlConnection.Dispose(); sqlGetProduct.Dispose(); } }
private void AddToCart(BLProduct product) { int quantityAdded = 1; List<BLCartProduct> cartList; if (Session["cartList"] != null) cartList = (List<BLCartProduct>)Session["cartList"]; else cartList = new List<BLCartProduct>(); bool alreadyInCart = false; foreach (BLCartProduct cartProduct in cartList) { if (cartProduct.Id == product.Id) { alreadyInCart = true; if ((quantityAdded + cartProduct.Quantity) > product.StockQuantity) quantityAdded = product.StockQuantity - cartProduct.Quantity; cartProduct.Quantity += quantityAdded; break; } } if (!alreadyInCart) cartList.Add(new BLCartProduct(id: product.Id, quantity: quantityAdded)); int cartCount = 0; if (Session["cartCount"] != null) cartCount = (int)Session["cartCount"]; cartCount += quantityAdded; Session["cartCount"] = cartCount; Session["cartList"] = cartList; Response.Redirect("Home.aspx"); }
public static List<BLProduct> RetrieveTopNPopularProductsFromDB(string databaseConnectionString, int numberOfProducts) { List<BLProduct> products = new List<BLProduct>(); SqlConnection sqlConnection = new SqlConnection(databaseConnectionString); SqlCommand sqlGetPopularProducts = new SqlCommand($"SELECT TOP {numberOfProducts} ID, CategoryID, Name, Description, Price, Popularity, StockQuantity, VAT FROM Products ORDER BY Popularity DESC", sqlConnection); SqlDataReader sqlReader = null; try { sqlConnection.Open(); sqlReader = sqlGetPopularProducts.ExecuteReader(); while (sqlReader.Read()) { BLProduct product = new BLProduct(id: int.Parse(sqlReader["ID"].ToString()), categoryId: int.Parse(sqlReader["CategoryID"].ToString()), name: sqlReader["Name"].ToString(), description: sqlReader["Description"].ToString(), price: double.Parse(sqlReader["Price"].ToString()), popularity: int.Parse(sqlReader["Popularity"].ToString()), stockQuantity: int.Parse(sqlReader["StockQuantity"].ToString()), VAT: double.Parse(sqlReader["VAT"].ToString())); product.GetDiscountFromDB(databaseConnectionString); product.GetTrailerUrlFromDB(databaseConnectionString); products.Add(product); } return products; } finally { if (sqlReader != null) { sqlReader.Close(); sqlReader.Dispose(); } sqlConnection.Close(); sqlConnection.Dispose(); sqlGetPopularProducts.Dispose(); } }