title: How to use OUTPUT parameter in Stored Procedure
body: <p>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.</p>
  
  <p>My Stored Procedure:</p>
  
  <pre><code>ALTER PROCEDURE selQuery
      (
          @id int, @code varchar(50) OUTPUT
      )
  AS
      SELECT RecItemCode = @code, RecUsername from Receipt where RecTransaction = @id
      RETURN @code
  </code></pre>
  
  <p>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."</p>
  
  <p>And I am using the Stored Procedure as:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>Error : "Object reference not set to an instance of an object."
  I want to get the value of output parameter. How to get that?</p>
  
  <p>Thanks.</p>