Dapper error : Each grid can only be iterated once

dapper

Question

My code is as below

   try
        {
            SqlConnection mapperConnection = SqlAccessHelper.SqlHelper.GetOpenConnection(SqlConnectionHelper.SqlConnectionString());
            var parameters = new DynamicParameters();
            parameters.Add("@P_MarketId", marketId, DbType.Int32);
            parameters.Add("@P_Output", dbType: DbType.Int32, direction: ParameterDirection.Output);

            using (var multi = mapperConnection.QueryMultiple("USP_FetchMarketRecords", parameters, (SqlTransaction)null, 1000000, CommandType.StoredProcedure))
            {
                    IEnumerable<MarketRecord.FItem> FItem = multi.Read<MarketRecord.FItem>();                        
                    IEnumerable<MarketRecord.FSubsystem> FSubsystem = multi.Read<MarketRecord.FSubsystem>();
                    objCResponseVO.addObject("FItem",FItem);
                    objCResponseVO.addObject("FSubsystem",FSubsystem);
            }
        }
        catch (Exception ex) {
            throw ex;
        }

first read is fine . Second read of FSubsystem gives me the exception "Each grid can only be iterated once". whats wrong here ? When I traced it down in public IEnumerable Read() method during second read consumed property is true . How can i overcome from this.?

Accepted Answer

The underlying data-reader is a forwards only device; by calling Read a second time, you move the reader forwards to the next grid. You should consume each grid in a forwards-only way. For example, you could re-order the statements:

using (var multi = mapperConnection.QueryMultiple("USP_FetchMarketRecords",
    parameters, (SqlTransaction)null, 1000000, CommandType.StoredProcedure))
{
        IEnumerable<MarketRecord.FItem> FItem =
            multi.Read<MarketRecord.FItem>();                        
        objCResponseVO.addObject("FItem",FItem);

        IEnumerable<MarketRecord.FSubsystem> FSubsystem =
            multi.Read<MarketRecord.FSubsystem>();            
        objCResponseVO.addObject("FSubsystem",FSubsystem);
}

Or you could buffer:

using (var multi = mapperConnection.QueryMultiple("USP_FetchMarketRecords",
    parameters, (SqlTransaction)null, 1000000, CommandType.StoredProcedure))
{
        IEnumerable<MarketRecord.FItem> FItem =
            multi.Read<MarketRecord.FItem>().ToList();
        IEnumerable<MarketRecord.FSubsystem> FSubsystem =
            multi.Read<MarketRecord.FSubsystem>().ToList();
        objCResponseVO.addObject("FItem",FItem);
        objCResponseVO.addObject("FSubsystem",FSubsystem);
}

Popular Answer

I just have passed through a similar issue. Mine is Postgresql instead of SQL Server but I think it doesn't matter on this case.

Adding a ToList() at the end of the result did the trick. Hope it works for you as well.

            string sql = @"
                    select * from usuario where id=@id;

                    select  idarea 
                    from    areausuario
                    where   idusuario = @id;

                    select * from area;
                    ";


        var res = this.Connection.QueryMultiple(sql, p);

        var usuarioData = res.Read<Entities.Usuario>().First();
        var areasSelected = res.Read<int>().ToList();
        var areas = res.Read<Entities.CatalogoPadreSelection>().ToList();


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why