title: SQL Server Express 2008, Joins, and Timeout Expired Error Message
body: <p>I have a stored procedure that does quite a bit of joins.  The query though runs pretty fast, around 3 seconds.  I just cant figure out the below error poping up every once in a while.  I event cache the document that uses this query for a minute so it doesnt get ran over and over. I am using Entity Framework 5, and my stored procedure is using a CTE to do the paging. Any clues or insight?</p>
  
  <pre><code> System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---&gt; System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
     at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
     at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady)
     at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
     at System.Data.SqlClient.SqlDataReader.get_MetaData()
     at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
     at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task&amp; task, Boolean asyncWrite)
     at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task&amp; task, Boolean asyncWrite)
     at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
     at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
     at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
     at System.Data.Common.DbCommand.ExecuteReader()
     at System.Data.Objects.ObjectContext.ExecuteStoreQueryInternal[TElement](String commandText, String entitySetName, MergeOption mergeOption, Object[] parameters)
     at System.Data.Objects.ObjectContext.ExecuteStoreQuery[TElement](String commandText, Object[] parameters)
     at System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery[TElement](String sql, Object[] parameters)
     at System.Data.Entity.Internal.InternalContext.ExecuteSqlQueryAsIEnumerable[TElement](String sql, Object[] parameters)
     at System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery(Type elementType, String sql, Object[] parameters)
     at System.Data.Entity.Internal.InternalSqlNonSetQuery.GetEnumerator()
     at System.Data.Entity.Internal.InternalSqlQuery`1.GetEnumerator()
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at Tournaments.Data.Repositories.Games.GamesRepository.GetGamesPaged(IGamesCriteria criteria)
  </code></pre>
  
  <p><strong>Entity Framework Method</strong></p>
  
  <pre><code> public PagedResult&lt;GameComplex&gt; GetGamesPaged(IGamesCriteria criteria)
          {
              var results = DataContext.Database.SqlQuery&lt;GameComplex&gt;("EXEC [Schema].[Database] @Page, @PageSize, @SortOrder, @SortDirection, @EventId, @DivisionId, @DivisionTeamId, @Date, @SearchToken, @MemberId",
                  new SqlParameter("Page", criteria.Page),
                  new SqlParameter("PageSize", criteria.PageSize),
                  new SqlParameter("SortOrder", GetDataValue(criteria.SortOrder)), 
                  new SqlParameter("SortDirection", GetDataValue(criteria.SortDirection)),
                  new SqlParameter("EventId", GetDataValue(criteria.EventId)),
                  new SqlParameter("DivisionTeamId", GetDataValue(criteria.DivisionTeamId)), 
                  new SqlParameter("DivisionId", GetDataValue(criteria.DivisionId)), 
                  new SqlParameter("Date", GetDataValue(criteria.Date)),
                  new SqlParameter("SearchToken", GetDataValue(criteria.SearchToken)),
                  new SqlParameter("MemberId", GetDataValue(criteria.MemberId))).ToList();
  
              return new PagedResult&lt;GameComplex&gt;
                         {
                             Page = criteria.Page,
                             PageSize = criteria.PageSize,
                             Total = results.Any(q =&gt; q != null) ? results.FirstOrDefault().Total : 0,
                             Results = results
                         };
          }
  </code></pre>
  
  <p><strong>SQL Server Stored Procedure Parameter Signature</strong></p>
  
  <pre><code>ALTER PROCEDURE [Schema].[Database]
      @Page INT = 1,
      @PageSize INT = 10,
      @SortOrder NVARCHAR(100) = 'Id',
      @SortDirection VARCHAR(4) = 'ASC',
      @EventId INT = NULL,
      @DivisionId INT = NULL,
      @DivisionTeamId INT = NULL,
      @Date DATETIME = NULL,
      @SearchToken NVARCHAR(100) = NULL,
      @MemberId INT = NULL
  AS
  </code></pre>