title: Stored procedure times out when ran from code, but not from query tool
body: <p>I am trying to figure out why a stored procedure call takes seconds in a SQL server express query window, but when I run call the stored procedure in code the query TIMES OUT.  We are using sql server 2008.  I know it is hard to say exactly what is going on without seeing the stored procedure.  I'm just hoping this is a known issue.  Any guidance is much appreciated.</p>
  
  <p>SQL query that calls "STORED_PROCEDURE_X" and runs in 2 seconds in SQL server express query window:</p>
  
  <pre><code>EXEC STORED_PROCEDURE_X '07/01/2010', '07/31/2010', 0, '', 'true','', 'Top 20'
  </code></pre>
  
  <p>Code that calls "STORED_PROCEDURE_X" and TIMES OUT:</p>
  
  <pre><code>SqlConnection connSQL = null;
  SqlCommand sqlCmd = null;
  SqlDataAdapter sqlDataAdpater = null;
  DataTable returnData = null;
  
  try
  {
      returnData = new DataTable();
      connSQL = new SqlConnection(sqlConnection);
      sqlCmd = new SqlCommand("STORED_PROC_X", connSQL);
      if (connSQL.State == ConnectionState.Closed)
      {
          connSQL.Open();
      }
      sqlCmd.CommandType = CommandType.StoredProcedure;
      sqlCmd.CommandTimeout = 600;
      sqlCmd.Parameters.Add("@StartDate", SqlDbType.NVarChar).Value = "07/01/2010";
      sqlCmd.Parameters.Add("@EndDate", SqlDbType.NVarChar).Value = "07/31/2010";
  
      sqlCmd.Parameters.Add("@AuditType", SqlDbType.Int).Value = "0";
  
      sqlCmd.Parameters.Add("@SortBy", SqlDbType.NVarChar).Value = "";
  
      sqlCmd.Parameters.Add("@IsClaimDepartment", SqlDbType.NVarChar).Value = "true";
      sqlCmd.Parameters.Add("@IdsList", SqlDbType.NVarChar).Value = "";
      sqlCmd.Parameters.Add("@ReportType", SqlDbType.NVarChar).Value = "Top 20";
      sqlDataAdpater = new SqlDataAdapter(sqlCmd);
      sqlDataAdpater.Fill(returnData);
      if (connSQL.State == ConnectionState.Open)
      {
          connSQL.Close();
      }
      return returnData;
  }
  catch (Exception ex)
  {
      LogErrorMessages("ExecuteStoredProcedure", ex.Message);
      throw ex;
  }
  </code></pre>
  
  <p><strong>Exception Received:</strong></p>
  
  <pre><code>System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
  </code></pre>