title: SQL output parameters in C#
body: <p>I'm trying to get a value from Microsoft SQL Server <code>stored procedure</code>. Totally I need to return two values. As you see below one of them is accessible by return and the other is accessible by a output parameter. I run this code in WPF(C#):</p>
  
  <pre><code>    public int InsertCustomerEntranceLogWithOptions(String cardNumber,int buy, int score, out int logID)
      {
          SqlCommand command = new SqlCommand(
              @"
              --declare @id int, @logID int                    
  
              exec @res = Insert_CustomerEntranceLog_WithOptions 
                  @cardNumber, @buy, @score, @logID
  
              --set @logID = @id",
              myConnection);
  
          command.CommandType = CommandType.Text;
          command.Parameters.Add("@cardNumber", SqlDbType.NVarChar).Value = cardNumber;
          command.Parameters.Add("@buy", SqlDbType.Int).Value = buy;
          command.Parameters.Add("@score", SqlDbType.Int).Value = score;
  
          command.Parameters.Add("@logID", SqlDbType.Int).Value = 0;
          command.Parameters["@logID"].Direction = ParameterDirection.Output;      
  
          command.Parameters.Add("@res", SqlDbType.Int).Value = 0;
          command.Parameters["@res"].Direction = ParameterDirection.Output;
  
          int res = (int)RunNonQuery(command);
          logID = (int)command.Parameters["logID"].Value;
  
          return res;
      }
  </code></pre>
  
  <p>and RunNonQuery Method is:</p>
  
  <pre><code>    public Object RunNonQuery(SqlCommand command, String returnVariableName = "@res")
      {
          Object result = null;
          if (openConnection())
          {
              try
              {
                  command.ExecuteNonQuery();
  
                  if (returnVariableName.Length != 0)
                  {
                      result = command.Parameters[returnVariableName].Value;
                      //command.Dispose();
                  }
                  else
                  {
                      //command.Dispose();
                      return 1;
                  }
              }
              catch (TimeoutException ex)
              {
                  //Log
              }
              catch (Exception ex)
              {
                  Utility.LogExceptionError(ex, 1, 1, 1);
                  //throw ex;
              }
  
              closeConnection();
          }
  
  
          return result;
      }
  </code></pre>
  
  <p>I'm sure that my <code>RunNonQuery</code> works properly. I test it a lot. But When I  <code>InsertCustomerEntranceLogWithOptions</code> method, I Get this <strong>error</strong>:</p>
  
  <pre><code>An unhandled exception of type 'System.IndexOutOfRangeException' 
  occurred in System.Data.dll
  
  Additional information: An SqlParameter with ParameterName 'logID' 
  is not contained by this SqlParameterCollection.
  </code></pre>
  
  <p>It seems that I do not add <code>SqlParameter</code> but as you see <code>LogID</code> is inserted. what is wrong?
  I also remove comments in SQL command and then run it but steel I see the error.</p>