Search code examples
.netsql-serverstored-proceduresado.netpetapoco

Proc Not Returning Output Parameter Value using PetaPoco


If I execute this from SQL Server, I get the output parameter with no problem:

exec [dbo].[SelectByRecipient] @0, @1, @2, @TotalCount = @3 output

The output parameter returns 18. However, if I do this from PetaPoco, the output parameter returns null. I tried these combinations:

var total = new SqlParameter("TotalCount", System.Data.SqlDbType.Int);
total.Direction = System.Data.ParameterDirection.Output;

var results = ctx.Query<X>("exec [dbo].[SelectByRecipient] @0, @1, @2, @TotalCount = @3 output", var0, var1, var2, total);
//var results = ctx.Query<X>("exec [dbo].[SelectByRecipient] @0, @1, @2, @@TotalCount = @3 output", var0, var1, var2, total);
//var results = ctx.Query<X>("exec [dbo].[SelectByRecipient] @0, @1, @2, @3 output", var0, var1, var2, total);
//var results = ctx.Query<X>("exec [dbo].[SelectByRecipient] @0, @1, @2, @3 out", var0, var1, var2, total);

//Blows up because total.Value is null
var totalCount = (int)total.Value;

The three statements I tried fail. They were working, but I don't know what changed... all of a sudden, I get these errors now...

Any ideas why?


Solution

  • The result worked once I called ToList(); I read somewhere that output parameters do not return the output value when the reader is still open, which had to be the case.