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

  • SEARCH
  • Home
  • 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 6724341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:40:42+00:00 2026-05-26T09:40:42+00:00

I am new to writing Stored Procedure. So I wrote one with output parameters

  • 0

I am new to writing Stored Procedure. So I wrote one with output parameters and want to access the output value, hot to do it.

My Stored Procedure:

ALTER PROCEDURE selQuery
    (
        @id int, @code varchar(50) OUTPUT
    )
AS
    SELECT RecItemCode = @code, RecUsername from Receipt where RecTransaction = @id
    RETURN @code

If trying to set “@code=RecItemCode” getting error as : “A SELECT statement that assigns a value to a variable must not be combined with Data Retrieval operations.”

And I am using the Stored Procedure as:

con.Open();
cmd.Parameters.AddWithValue("@id", textBox1.Text);
SqlParameter code = new SqlParameter("@code", SqlDbType.Int);
code.Direction = ParameterDirection.Output;
cmd.Parameters.Add(code);
SqlDataReader sdr = cmd.ExecuteReader();
MessageBox.Show(cmd.Parameters["@code"].Value.ToString()); // getting error
con.Close();

Error : “Object reference not set to an instance of an object.”
I want to get the value of output parameter. How to get that?

Thanks.

  • 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-26T09:40:43+00:00Added an answer on May 26, 2026 at 9:40 am

    The SQL in your SP is wrong. You probably want

    Select @code = RecItemCode from Receipt where RecTransaction = @id
    

    In your statement, you are not setting @code, you are trying to use it for the value of RecItemCode. This would explain your NullReferenceException when you try to use the output parameter, because a value is never assigned to it and you’re getting a default null.

    The other issue is that your SQL statement if rewritten as

    Select @code = RecItemCode, RecUsername from Receipt where RecTransaction = @id
    

    It is mixing variable assignment and data retrieval. This highlights a couple of points. If you need the data that is driving @code in addition to other parts of the data, forget the output parameter and just select the data.

    Select RecItemCode, RecUsername from Receipt where RecTransaction = @id
    

    If you just need the code, use the first SQL statement I showed you. On the offhand chance you actually need the output and the data, use two different statements

    Select @code = RecItemCode from Receipt where RecTransaction = @id
    Select RecItemCode, RecUsername from Receipt where RecTransaction = @id
    

    This should assign your value to the output parameter as well as return two columns of data in a row. However, this strikes me as terribly redundant.

    If you write your SP as I have shown at the very top, simply invoke cmd.ExecuteNonQuery(); and then read the output parameter value.


    Another issue with your SP and code. In your SP, you have declared @code as varchar. In your code, you specify the parameter type as Int. Either change your SP or your code to make the types consistent.


    Also note: If all you are doing is returning a single value, there’s another way to do it that does not involve output parameters at all. You could write

     Select RecItemCode from Receipt where RecTransaction = @id
    

    And then use object obj = cmd.ExecuteScalar(); to get the result, no need for an output parameter in the SP or in your code.

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

Sidebar

Related Questions

I'm writing a stored procedure and I want to return 0 records when something
I'm new to this. I'm writing a clr stored procedure that calls another stored
I have a stored procedure which is returning me about 50 columns. I want
I'm writing a stored procedure that should sync a table from our production environment
please help me with writing this search sql stored procedure procedure may have different
I'm writing a stored procedure in SQL Server 2008, interatively. When using SQL Server
I am having trouble writing a stored procedure that first checks a hashed password
I'm writing a stored procedure to update multiple records based on a table variable
Iam asking this because iam very much new to stored procedure. Can any help
Hi I am writing a large stored procedure, which creates a dynamic report table,

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.