Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 336895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:17:07+00:00 2026-05-12T10:17:07+00:00

I have created a stored procedure similar to this simplified example: CREATE PROCEDURE dbo.sp_MyStoredProcedure

  • 0

I have created a stored procedure similar to this simplified example:

CREATE PROCEDURE dbo.sp_MyStoredProcedure
    @Var1 INT OUTPUT,
    @Var2 DECIMAL(10,2) OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
        @Var1 = COUNT(*),
        @Var2 = SUM(TranAmount)
    FROM
        MyTable

    SELECT * FROM MyTable
END

When I try to read the values from the output variables after I call the ExecuteReader() method of the SqlCommand object, the values are null.

string MyConnString = string.Empty;
SqlConnection MyConn = new SqlConnection(MyConnString);
SqlCommand MyCmd = new SqlCommand("sp_MyStoredProcedure", MyConn);
MyCmd.CommandType = CommandType.StoredProcedure;
MyCmd.Parameters.Add(new SqlParameter("@Var1", SqlDbType.Int));
MyCmd.Parameters.Add(new SqlParameter("@Var2", SqlDbType.Decimal);
MyCmd.Parameters[0].Direction = ParameterDirection.Output;
MyCmd.Parameters[1].Direction = ParameterDirection.Output;
SqlDataReader dr = MyCmd.ExecuteReader(CommandBehavior.CloseConnection);
int Var1 = Convert.ToInt32(MyCmd.Parameters[0].Value);
decimal Var1 = Convert.ToDecimal(MyCmd.Parameters[1].Value);

What I am doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:17:07+00:00Added an answer on May 12, 2026 at 10:17 am

    You need to read the reader till end, the output parameters are at the end of the TDS stream and the client won’t see them until the result set isn’t consumed.

    If you must have the count and sum before you read the resultset you must ditch the OUTPUT params. Just produce an ordinary result set with the two values you’re interested in folowed buy the SELECT * result set. Then read both result sets in the client using SqlDataReader.NextResult().

    Update

    Here is what I mean by having two results set:

    CREATE PROCEDURE dbo.sp_MyStoredProcedure    
    AS
    BEGIN    
      SET NOCOUNT ON;    
      SELECT COUNT(*) as cnt, SUM(TranAmount) as sum_ta
      FROM MyTable
      SELECT * FROM MyTable
    END
    

    and the client:

    string MyConnString = string.Empty;
    SqlConnection MyConn = new SqlConnection(MyConnString);
    SqlCommand MyCmd = new SqlCommand("sp_MyStoredProcedure", MyConn);
    MyCmd.CommandType = CommandType.StoredProcedure;
    using(SqlDataReader dr = MyCmd.ExecuteReader(CommandBehavior.CloseConnection))
    {
      while(dr.Read())
      { 
         count = dr["cnt"];
         sum = dr["sum_ta"];
      }
      dr.NextResult();
      while(dr.Read())
      {
        // process MyTable row here
      }
    }
    

    Note that you do not need to do this if your C# code does not need the value of the output parameters before consuming the data reader. You can simply read the SqlDataReader till end and then check the output parameters, they will be set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure :- CREATE procedure St_Proc_GetTimeEntryID @userID int, @timeEntryID int output
I have many similar structure tables like this: CREATE TABLE [dbo].[tbl_Hierarchy]( [ID] [int] NOT
I have this stored procedure: CREATE OR REPLACE PROCEDURE LIQUIDACION_OBTENER ( p_Cuenta IN NUMBER,
I have this stored procedure for Oracle: create or replace procedure bns_saa_message_get() <--- PROBLEM
I have 3 tables similar to the sctructure below CREATE TABLE [dbo].[EmpBasic]( [EmpID] [int]
I have a sql stored procedure similar to below : create procedure procedurename declare
I have just created a report in Report Manager using a Stored Procedure which
I have a dynamic SQL statement I've created in a stored procedure. I need
I have following stored procedure: CREATE PROCEDURE testProc(IN p_idProject INTEGER) BEGIN DECLARE nowTime DATETIME;
I have the fallowing stored procedure: CREATE PROCEDURE `get`(IN tb VARCHAR(50), IN id INTEGER)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.