Ado.net running SQL Server stored procedure can't retrieve output value when using ExecuteReader

102 views Asked by At
param.Add(new SqlParameter("@Count", SqlDbType.Int) { Direction = ParameterDirection.Output });

using(var dataset = MsSqlHelper.ExecuteDataset(SqlConn.Conn, CommandType.StoredProcedure, "GetList", param.ToArray()))
{
    // Count.Value is not NULL
}

using (SqlDataReader dr = MsSqlHelper.ExecuteReader(SqlConn.Conn, CommandType.StoredProcedure, "GetList", param.ToArray()))
{
    // Count.Value is NULL    
}

There is an output value in my stored procedure.

If I use ExecuteReader, the value of param Count is null. But if I change method ExecuteReader to ExecuteDataset, then I can get value of Count.

Why is there no value when I use ExecuteReader?

By the way, nothing has changed in my example except ExecuteDataset / ExecuteReader.

1

There are 1 answers

1
Joe On

You should check the output parameter value after the reader has been closed (i.e. after the using block), or after reading to the end of the data. See this KB article.