IDataReader Suddenly Closing & Throwing Exception In Custom Call

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
14 лет назад
The nopCommerce Code which I have replaced:
/// <summary>
        /// Gets product
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <returns>Product</returns>
        public override DBProduct GetProductByID(int ProductID)
        {
            DBProduct product = null;
            if (ProductID == 0)
                return product;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    product = GetProductFromReader(dataReader);
                }
            }
            return product;
        }


with custom function below:


public override DBProduct GetProductByDate()
        {
            DBProduct product = null;
            
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductLoadByDate");
        
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    product = GetProductFromReader(dataReader);
                }
            }
            return product;
        }



The first function GetProductByID(Int ProductID) works fine. The modified function GetProductByDate() throws an error.

The dbCommand Object and the db object get properly instantiated, but when referred to dataReader.Read the reader shows itself as closed & throws an error:
"Invalid attempt to call Read when reader is closed."
However in the line before the if statement: the data reader is open and contains data.

This is puzzling to me as I have not modified any of the data reader code and the data reader is declared with a "using" statement which should ensure that it is disposed at the end of the statement.

I would very much appreciate any help!

Thanks in advance...
14 лет назад
Hi, can I see exception details.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.