We are using a custom Membership Provider in an ASP.NET MVC application. We have been experiencing intermittent 'System.InvalidOperationException - There is already an open DataReader associated with this Command which must be closed first.' problems with the class so I decided to enabled MARS.
This is the connection string in the web config...
<add name="CustomMembershipServices" connectionString="User ID=CUSTOM_USER;Password=pwd;Database=OUR_DB;Server=.\SQLEXPRESS;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
Our code now throws an ArgumentException: Keyword not supported: 'multipleactiverecordsets' at the following line..
protected void CreateAndOpenConnection()
{
// Exception thrown here...
_connection = new SqlConnection(_connectionString);
_connection.Open();
}
The same exception is seen when targeting SQL2005 Express and SQL2008 Standard editions. Any suggestions?
In response to AdaTheDev here is the method that uses the data reader...
public bool CheckUserPassword(long userID, string password)
{
bool success = false;
if (!string.IsNullOrEmpty(password))
{
try
{
CreateAndOpenConnection();
using (SqlCommand cmd = CreateSqlCommandForStoredProcedure("CheckPassword"))
{
IPasswordUtility generator = new PasswordUtility();
cmd.Parameters.AddWithValue("UserID", userID);
cmd.Parameters.AddWithValue("Password", generator.HashPassword(password));
using (SqlDataReader reader = cmd.ExecuteReader())
{
success = reader.HasRows;
reader.Close();
}
}
}
finally
{
CloseConnection();
}
}
return success;
}
I cannot see any reason why this would be creating multiple data readers on the same connection.