title: Sql error "Procedure or function expects parameter , which was not supplied" when calling a stored procedure from c# code
body: <p>I have a stored procedure that takes three parameters: </p>
  
  <ol>
  <li>AccountId </li>
  <li>StartDate </li>
  <li>EndDate</li>
  </ol>
  
  <p>When I execute it directly from SQL Server Management Studio, it does not show any error, but when I call it from my application I get this error </p>
  
  <blockquote>
    <p>Procedure or function 'GetSubscribersForAccountID_With_Dates' expects
    parameter '@AccountId', which was not supplied.</p>
  </blockquote>
  
  <p>Here is the stored procedure :</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[GetSubscribersForAccountID_With_Dates]
      @AccountId  as Int ,
      @StartDate DATETIME = null,
      @EndDate DATETIME = null
  AS
  BEGIN
      SET NOCOUNT ON;
  
       --- BODY ---
      DECLARE @SQL AS NVARCHAR(MAX)
  
      SET @SQL =' SELECT   phone_num AS MSISDN1,'
          +' number AS MSISDNID,'
          + 'parentaccount AS PARENT_ACCOUNT,'
          + 'account_no AS AccountID,'
         + 'service_fname AS FirstName,'
         + 'service_lname AS LastName,'
        + ' service_active_dt AS startdate,'
         + 'service_inactive_dt AS enddate '
     + ' FROM     [myTable]'
     + ' WHERE    parent_account =  ' + CONVERT(VARCHAR, @AccountId)
  
        If (@StartDate is not null And @EndDate is not null)
        Begin
            SET @SQL += ' AND service_active_dt &lt;= '''+ CONVERT(VARCHAR(20),   @EndDate,101)
            SET @SQL += ''' AND  (service_inactive_dt is null or service_inactive_dt &gt;= ''' + CONVERT(varchar, @StartDate,101)+''')'
        End
        Else 
            SET @SQL += ' AND service_inactive_dt is  null ' 
  
        --Debug
        print(@SQL)
  
      EXEC SP_EXECUTESQL @SQL
  END
  </code></pre>
  
  <p>The method that calls the stored procedure:</p>
  
  <pre><code> public List&lt;MSISDN&gt; GetListOfSubscribersByAccountID_WithDates(int accountID, DateTime? from = null, DateTime? to = null) 
   {
          List&lt;MSISDN&gt; lstMsisdn = new List&lt;MSISDN&gt;();
          string spName = "GetSubscribersForAccountID_With_Dates";
  
          IDbCommand cmd = CreateCommand(spName);  
  
          //Adding parameters
  
          DbParameter param;
          param = new SqlParameter("@AccountId", accountID);
          param.DbType = DbType.Int32;
          param.Direction = ParameterDirection.Input;
          cmd.Parameters.Add(param);
  
          if (from != null)
          {
              param = new SqlParameter("@StartDate", from);
              param.DbType = DbType.DateTime;
              param.Direction = ParameterDirection.Input;
              cmd.Parameters.Add(param);
          }
  
          if (to != null)
          {
              param = new SqlParameter("@EndDate", to);
              param.DbType = DbType.DateTime;
              param.Direction = ParameterDirection.Input;
              cmd.Parameters.Add(param);
          }
  
  
  
          try
          {
              using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleResult))
              {
                  while (reader.Read())
                  {
                      lstMsisdn.Add(GetSubscribersFromReader(reader));
                  }
              }
  
              return lstMsisdn;
  
          }
          catch (Exception ex)
          {
  
              throw ex;
          }
          finally
          {
              if (this.Connection != null)
                  this.Connection.Close();
          }
      }
  </code></pre>