title: Get Output Parameter Value in C#
body: <p>I have a stored procedure with an OUTPUT parameter:</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[myStoredProcedure]
      (@myValue NVARCHAR(100) OUTPUT)
  AS
  BEGIN
      SET @myValue = (SELECT myValue FROM myTable WHERE something = 1)
      SElECT @myValue;
  END
  </code></pre>
  
  <p>My C# class:</p>
  
  <pre><code>public string getOutPut()
  {
      string shortFooter;
      string sql = "myStoredProcedure";
      string connStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  
      using (SqlConnection DBConn = new SqlConnection(connStr))
      {
          using (SqlCommand DBCmd = new SqlCommand(sql, DBConn))
          {
              try
              {
                  DBConn.Open();
  
                  DBCmd.CommandType = CommandType.StoredProcedure;
                  SqlParameter newSqlParam = new SqlParameter();
                  newSqlParam.ParameterName = "@myValue";
                  newSqlParam.SqlDbType = SqlDbType.NVarChar;
                  newSqlParam.Direction = ParameterDirection.Output;
                  DBCmd.Parameters.Add(newSqlParam);
  
                  DBCmd.ExecuteNonQuery();
                  shortFooter = DBCmd.Parameters["@myValue"].Value.ToString();
              }
              catch (Exception ex)
              {
                  string blah = ex.ToString();
                  shortFooter = "";
              }
          }
      }
  
      return shortFooter;
  }
  </code></pre>
  
  <p>This is the exception that I'm getting:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: String[0]: the Size property has an
    invalid size of 0.</p>
  </blockquote>
  
  <p>What I don't understand is, I have used this code before, except that in the stored procedure I'm getting the value of the output with <code>IDENTITY()</code> when I create a new record in the database, so the parameter type in the class is INT. But in concept it should be the same, but I guess I'm wrong, just don't know why.</p>
  
  <p>What am I missing?</p>
  
  <p>Thanks!!</p>