Returning a list from a Dapper stored procedure

c# dapper

Question

I have a dapper method, that I want to store against a class.

        using (var block = new TransactionBlock())
        {
            const string sql =
                "select * from Messages where statusId not in ('5','6')";
            var results = TransactionBlock.Connection.Query<OpenMessages>
            (sql,TransactionBlock.Transaction).ToList();
            block.Commit();
            return results;
        }

So, OpenMessages is :

   public class OpenMessages
   {
      public List<string> MessageId { get; set; }
   }

The following error is being returned:

System.InvalidOperationException was unhandled by user code HResult=-2146233079 Message=ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.

I have several other methods, that are working fine, just not this one that I need to return a list.

Accepted Answer

You are passing the transaction in as the parameters. Name it:

var results = TransactionBlock.Connection.Query<OpenMessages>
        (sql,
   transaction: TransactionBlock.Transaction).ToList();

Also, if it really is a stored procedure you probably want to add:

commandType: CommandType.StoredProcedure


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