//_____________________________________________________________________ /// <summary> /// This method returns the corresponding category id from the category name. /// </summary> /// <param name="catname"></param> /// <param name="subcatname"></param> /// <returns></returns> public static Categories GetCategoryIdByName(string catname, string subcatname) { Categories catobj = new Categories(); Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString"); DbCommand cmdObj = _db.GetStoredProcCommand("GetCategoryIdByName"); _db.AddInParameter(cmdObj, "@CategoryName", DbType.String, catname); _db.AddInParameter(cmdObj, "@SubCategoryName", DbType.String, subcatname); using (IDataReader dataReader = _db.ExecuteReader(cmdObj)) { while (dataReader.Read()) { catobj.CategoryId = Convert.ToInt32(dataReader["CategoryId"]); catobj.SubCategoryId = Convert.ToInt32(dataReader["SubCategoryId"]); } return catobj; } }
//_____________________________________________________________________ /// <summary> /// Gets the details of the particular product through its ProductId. /// </summary> /// <param name="productid"></param> /// <returns></returns> public static ProductCategoryRelation GetProduct(int productid) { ProductCategoryRelation pc = new ProductCategoryRelation(); Product prod = new Product(); Categories cat = new Categories(); prod.ProductId = productid; Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString"); DbCommand cmdObj = _db.GetStoredProcCommand("GetProductById"); _db.AddInParameter(cmdObj, "@ProductId", DbType.Int32, Convert.ToInt32(productid)); using (IDataReader dataReader = _db.ExecuteReader(cmdObj)) { while (dataReader.Read()) { prod.ProductId = Convert.ToInt32(dataReader["ProductId"]); prod.ProductName = dataReader["ProductName"].ToString(); prod.ProductDescription = dataReader["ProductDescription"].ToString(); prod.CategoryId = Convert.ToInt32(dataReader["CategoryId"]); prod.SubCategoryId = Convert.ToInt32(dataReader["SubCategoryId"]); prod.Price = Double.Parse(dataReader["Price"].ToString()); prod.UnitsInStock = Convert.ToInt32(dataReader["UnitsInStock"]); prod.StockAvailability = Convert.ToInt32(dataReader["StockAvailability"]); prod.Colour = dataReader["Colour"].ToString(); prod.Size = dataReader["Size"].ToString(); prod.Picture = dataReader["Picture"].ToString(); } } DbCommand cmdObj2 = _db.GetStoredProcCommand("GetCategoryNameById"); _db.AddInParameter(cmdObj2, "@CategoryId", DbType.Int32, Convert.ToInt32(prod.CategoryId)); _db.AddInParameter(cmdObj2, "@SubCategoryId", DbType.Int32, Convert.ToInt32(prod.SubCategoryId)); using (IDataReader dataReader = _db.ExecuteReader(cmdObj2)) { while (dataReader.Read()) { cat.CategoryName = dataReader["CategoryName"].ToString(); cat.SubCategoryName = dataReader["SubCategoryName"].ToString(); } } pc.product = prod; pc.categories = cat; return pc; }
//_______________________________________________________________________ /// <summary> /// This method returns a list of all the different categories in the database. /// </summary> /// <returns></returns> public static List<string> GetAllCategories() { var catnames = new List<string>(); Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("LetsShopConnString"); DbCommand cmdObj = _db.GetStoredProcCommand("GetAllCategories"); using (IDataReader dataReader = _db.ExecuteReader(cmdObj)) { while (dataReader.Read()) { var cat = new Categories(); cat.CategoryName = dataReader["CategoryName"].ToString(); catnames.Add(cat.CategoryName); } return catnames; } }