title: Calling a parameterized Stored Procedure that returns a value in C#
body: <p>C# --> I have a parameterized stored procedure (SP) that returns an int that I want to call and read the return value that I want to assign to a hidden field.  I believe I have most of it written but am stuck on the final part (actually extracting the returned value from the SP.  Help would be much appreciated.  Here is what I have so far...</p>
  
  <pre><code>public int CheckIP()
  {
      string conn = "";
      conn = ConfigurationManager.ConnectionStrings["Connection"].ToString();
      SqlConnection sqlconn = new SqlConnection(conn);
      try
      {
          sqlconn.Open();
          DataSet ds = new DataSet();
          SqlCommand objcmd = new SqlCommand("sp_CheckIP", sqlconn);
          objcmd.CommandType = CommandType.StoredProcedure;
          SqlParameter IPAddress = objcmd.Parameters.Add("@IpAddress",
              SqlDbType.VarChar);
          IPAddress.Value = 0;
  
          SqlDataAdapter objAdp = new SqlDataAdapter(objcmd);
          objAdp.Fill(ds);
  
          ????
      }
      catch (Exception ex)
      {
          Response.Write(ex.Message.ToString());
      }
      finally
      {
          sqlconn.Close();
      }
  }
  </code></pre>
  
  <p>And my Stored Procedure is as follows:</p>
  
  <pre><code>ALTER Procedure [dbo].[sp_CheckIP]
  @IpAddress varchar(15),
  @AllowedLogInAttempts int Output
  
  AS
  BEGIN
  
  IF (Select Count(IPAddressID) From Emerald.dbo.IPManager 
      Where IPAddress = @IPAddress) = 1
  BEGIN
  
  Select @AllowedLogInAttempts = 
       CASE
          When Trusted = 0 and Block = 0 then '5'
          When Trusted = 1 and Block = 0 then '7'
          When Trusted = 0 and Block = 1 then '0'
          When Trusted = 1 and Block = 1 then '0'
          End 
  FROM Emerald.dbo.IPManager
  Where IPAddress = @IPAddress
  
  END
  ELSE 
  Select 3
  END
  </code></pre>