Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

The reader has been disposed with version 2.0.123

Open Dzep opened this issue 3 years ago • 3 comments

Hi i have problem and i was searching on internet but not found the correct answer. Can you tell me how to solve it. When i execute the first result i ok and after that i got the error message "The reader has been disposed; this can happen after all data has been consumed" , i also try .ToList() but the same error occur result.InfozasitePcja = (await res.ReadAsync<InfozasitePcja>()).ToList(); Because it is IEnumerable i can not use .ToListAsync(). Here is my code.

public async Task<GetInfoForUserSummary> GetInfoForUser(string column, string search)
{
    GetInfoForUserSummary result = new GetInfoForUserSummary();
    try
    {
        var query = "Select i.*,s.*,u.*,e.* From InfoZaSitePCja i " +
            "INNER JOIN SEPreport s ON i.ComputerName = s.COMPUTER_NAME " +
            "INNER JOIN Users u ON s.CURRENT_LOGIN_USER = u.SamAccountName " +
            "INNER JOIN Eerv e ON u.EmployeeNumber = e.[kadr.br.] " +
            "Where [" + column + "] LIKE '%' + @search + '%'";
        var param = new DynamicParameters();
        param.Add("@search", search, DbType.String);
        var res = await Connection.QueryMultipleAsync(query, param, commandType: CommandType.Text, transaction: Transaction);

        result.InfozasitePcja = await res.ReadAsync<InfozasitePcja>();
        result.SEPreport = await res.ReadAsync<SEPreport>();
        result.Users = await res.ReadAsync<Users>();
        result.Eerv = await res.ReadAsync<Eerv>();
    }
    catch (Exception e)
    {
        _logger.Error(e);
        throw new Exception("Database Error", e);
    }
    return result;
}

Dzep avatar Sep 02 '22 13:09 Dzep

What is Connection here? What is the lifetime, and is it perhaps shared with other consumers (which would be bad)? Also, relatively minor here, but res needs a using

mgravell avatar Sep 02 '22 20:09 mgravell

I think this is is confusion between .Query and .QueryMultiple. When using QueryMultiple and a grid reader, it's trying to read n distinct result sets, e.g. from:

Select * From Table1;
Select * From Table2;

...etc. In this case you're only getting back 1 result set in 1 big query (with indeterminate cardinality). You'll want to issue n queries with the same predicate (all in the same string) to .QueryMultiple here.

NickCraver avatar Sep 03 '22 13:09 NickCraver

Hello, here is the constructor and dependency injection protected readonly IDbTransaction Transaction; protected readonly IDbConnection Connection; private ILogger _logger; protected readonly IHttpContextAccessor _httpContextAccessor; public ReportsCommands(IUnitOfWork unitOfWork, ILogger logger, IHttpContextAccessor httpContextAccessor) { Connection = unitOfWork.Transaction.Connection; Transaction = unitOfWork.Transaction; _logger = logger; _httpContextAccessor = httpContextAccessor; } . When i try with with QueryAsync the return results for all entities are null. Also my summary entity class public partial class GetInfoForUserSummary { public IEnumerable<InfozasitePcja> InfozasitePcja { get; set; } public IEnumerable<SEPreport> SEPreport { get; set; } public IEnumerable<Users> Users { get;set; }
public IEnumerable<Eerv> Eerv { get;set;} }

Dzep avatar Sep 05 '22 07:09 Dzep

I think your query would return single dataset which means it wont return multiple tables. Just one table with all the data. QueryMultipleAsync ideally should be used when you are returning data as multiple tables. Since there is just one table returning, dapper has no further data to map to other data models. If this is the query as per the requirement then you should have all the properties in one single data model.

Bosegeek avatar Sep 24 '22 08:09 Bosegeek