About once a day or two now my web site starts throwing and error when people try to login and no one can log in until I restart IIS. How can Any() do this and what can I do to fix it?
Here’s the function throwing the exception:
public override bool ValidateUser(string username, string password)
{
var db = Access.Context;
var query =
from p in db.Players
where p.PlayerName == username
&& p.Password == password
select p
;
return query.Any();
}
Other LINQ to SQL queries work fine. Here’s the stack trace.
InvalidOperationException: Sequence contains no elements
System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +1151
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +113
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +344
System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +23
System.Linq.Queryable.Any(IQueryable`1 source) +243
FortyTwoAccess.PlayerProvider.ValidateUser(String username, String password) in C:\dev\FortyTwo\FortyTwoAccess\PlayerProvider.cs:114
FortyTwoClient.Models.AccountMembershipService.ValidateUser(String userName, String password) in C:\dev\FortyTwo\FortyTwoClient\Models\AccountModels.cs:117
FortyTwoClient.Controllers.AccountController.LogOn(LogOnModel model, String returnUrl) in C:\dev\FortyTwo\FortyTwoClient\Controllers\AccountController.cs:42
lambda_method(Closure , ControllerBase , Object[] ) +157
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
The fix for this was to not use a class variable to save the context and instead instantiate the context locally in each method wrapped in a using.