title: Getting error in calling Store procedure
body: <p>I have a SP that works when I execute from SQL Server Management Studio. However, when I call from my code:  </p>
  
  <pre><code>Database.SqlQuery&lt;Response&gt;("cw.SpGetCompanyLimits @user_id, @company_id",
      new SqlParameter("@user_id", userId),
      new SqlParameter("@company_id", companyId)).Single();
  </code></pre>
  
  <p>I get the exception: <code>InvalidOperationException: The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.</code></p>
  
  <p>I don't understand why this is happening, because my variables are <code>int</code> and they are defined as <code>int</code> in the SP as well.</p>
  
  <p>The SP: </p>
  
  <pre><code>CREATE PROCEDURE cw.SpGetCompanyLimits
      @company_id             INT=2,
      @user_id                INT=13
  AS BEGIN
  
  DECLARE
  @exchange_rate          DECIMAL(19,2),
  @dollars_auth_amount    DECIMAL(19,2),
  @bols_auth_amount       DECIMAL(19,2),
  @auth_limit             DECIMAL(19,2),
  @dollars_amount         DECIMAL(19,2),
  @bols_amount            DECIMAL(19,2),
  @company_limit          DECIMAL(19,2),
  @spent_bols             DECIMAL(19,2),
  @spent_dollars          DECIMAL(19,2),
  @current_date           CHAR(8)
  
  SELECT DISTINCT @auth_limit = Amount FROM Limits WHERE Id = @user_id
  
  SELECT @company_limit = DailyLimit FROM Companies WHERE Id = @company_id
  
  SELECT TOP 1  @exchange_rate = Sale FROM ExchangeRates ORDER BY Date DESC
  
  SET @current_date = CONVERT(CHAR(8), GETDATE(), 112)
  
  SELECT @dollars_amount = COALESCE(SUM(CASE WHEN Currency = 'USD' THEN Amount END), 0.00) ,
     @bols_amount = COALESCE(SUM(CASE WHEN Currency = 'BOL' THEN Amount END), 0.00)          
  FROM ProcessBatches
  WHERE CompanyId = @company_id AND
    CAST(DateProcess as date) = @current_date AND
    Currency IN ('BOL', 'USD') AND
    OperationTypeId NOT IN (17, 18) AND
    OperationStatusId IN (3, 4, 5, 6);
  
  SELECT @dollars_auth_amount = COALESCE(SUM(CASE WHEN Currency = 'USD' THEN Amount END), 0.00) ,
     @bols_auth_amount = COALESCE(SUM(CASE WHEN Currency = 'BOL' THEN Amount END), 0.00)          
  FROM ProcessBatches PB, AuthorizedBatches AB
  WHERE CompanyId = @company_id AND
    CAST(DateProcess as date) = @current_date AND
    Currency IN ('BOL', 'USD') AND
    AB.Id = @user_id AND
    OperationTypeId NOT IN (17, 18) AND
    PB.OperationStatusId IN (3, 4, 5, 6);
  
  SELECT 
      @company_limit AS CompanyLimit,
      @auth_limit AS AuthorizerLimit,
  CONVERT(DECIMAL(19,2), (@company_limit - (@dollars_amount + @bols_amount / @exchange_rate))) AS Available,
  CONVERT(DECIMAL(19,2), (@dollars_amount + @bols_amount / @exchange_rate)) AS SpentMoney,
  CONVERT(DECIMAL(19,2), (@dollars_auth_amount + @bols_auth_amount / @exchange_rate)) AS AuthorizedLimit,
  @bols_amount AS BolsCompany,
  @dollars_amount AS DollarsCompany
  END
  </code></pre>