title: C# Stored procedure or function expects parameter which is not supplied
body: <p>I am fairly new to C# and I'm trying to set up call to a stored procedure in my database which takes one parameter. </p>
  
  <p>I get the error "Procedure or function 'SP_getName' expects parameter '@username', which was not supplied. "</p>
  
  <p>My Stored procedure works ok when I supply it with the parameter and I run it via SQL management studio. </p>
  
  <pre><code>GO
  
  DECLARE @return_value int
  
  EXEC    @return_value = [dbo].[SP_getName]
      @username = 'bob101'
  
  SELECT  'Return Value' = @return_value
  
  GO
  </code></pre>
  
  <p>However when I try and call it the error is with how I'm passing the parameter in, but I can't spot what the issue is. </p>
  
  <pre><code>           //create a sql command object to hold the results of the query
              SqlCommand cmd = new SqlCommand();
  
              //and a reader to process the results
              SqlDataReader reader;
  
              //Instantiate return string
              string returnValue = null;
  
              //execute the stored procedure to return the results
              cmd.CommandText = "SP_getName";
  
              //set up the parameters for the stored procedure
              cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = "bob101";
  
              cmd.CommandType = CommandType.Text;
              cmd.Connection = this.Connection;
  
              // then call the reader to process the results
              reader = cmd.ExecuteReader();
  </code></pre>
  
  <p>Any help in spotting my error would be greatly appreciated!</p>
  
  <p>I've also tried looking at these two posts, but I haven't had any luck: </p>
  
  <p><a href="https://stackoverflow.com/questions/19703653/stored-procedure-or-function-expects-parameter-which-is-not-supplied">Stored procedure or function expects parameter which is not supplied</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/11900301/procedure-or-function-expects-parameter-which-was-not-supplied">Procedure or function expects parameter, which was not supplied</a></p>
  
  <p>Thanks!</p>