Intermittent exception

Topics: General Topic, NetSqlAzMan Core
Mar 20, 2012 at 11:18 AM
Edited Mar 20, 2012 at 11:20 AM

Hi there - I was wondering if you'd seen anything like this. When we create a new authorization we're sometimes seeing an exception occur.

The exception we see is:

There is already an open DataReader associated with this Command which must be closed first. Stack Trace:   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)   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.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)   at NetSqlAzMan.SqlAzManItem.CreateAuthorization(IAzManSid owner, WhereDefined ownerSidWhereDefined, IAzManSid sid, WhereDefined sidWhereDefined, AuthorizationType authorizationType, Nullable`1 validFrom, Nullable`1 validTo) in C:\SVNRoot\NetSqlAzman\NetSqlAzMan_Solution\NetSqlAzMan\SqlAzManItem.cs:line 780   at MppGlobal.Security.AuthorizationManager.CreateAuthorization(IAzManItem Item, String UserName, AuthorizationType AuthType) in c:\Projects\iPay_P21\_MppGlobal.Core\Security\AuthorizationExtensions.cs:line 273   at MppGlobal.iShop.Web.Management.EmbeddedBasePage.CreateNewSystemAccount(GetUserInfoResult salesforceUser) in c:\Projects\iPay_P21\MppGlobal.iShop\Web\Management\EmbeddedBasePage.cs:line 331   at MppGlobal.iShop.Web.Management.EmbeddedBasePage.OnPreLoad(EventArgs e) in c:\Projects\iPay_P21\MppGlobal.iShop\Web\Management\EmbeddedBasePage.cs:line 159

And here's our code that creates the authorization:

public IAzManAuthorization CreateAuthorization(IAzManItem Item, string UserName, AuthorizationType AuthType)
            var userIdentity = ((Thread.CurrentPrincipal.Identity as WindowsIdentity) ?? WindowsIdentity.GetCurrent());
            IAzManSid ownerSid = new SqlAzManSID(userIdentity.User.Value);
            IAzManDBUser dbUser = default(IAzManDBUser);
            using (var storage = new SqlAzManStorage(this.storageConnectionString))
                dbUser = storage.GetDBUser(UserName);

                IAzManAuthorization result = Item.CreateAuthorization(ownerSid, WhereDefined.LDAP, dbUser.CustomSid,
                                                                      WhereDefined.Database, AuthType, DateTime.Now,
                ExpirableStorageCache.AddItem(AuthorizationManager.STORE_NAME, AuthorizationManager.APPLICATION_NAME, Item);
                return result;

Mar 20, 2012 at 11:21 AM


if you are invoking this from an ASP.NET application (that is multi-thread) you cannot reuse the same SqlAzManStorage instance because it’s not thread-safe.

Andrea Ferendeles
NetSqlAzMan Project Coordinator
E-mail Web

Mar 20, 2012 at 11:43 AM

Thanks for the info - it's a bit confusing though since our SqlAzmanStorage is wrapped in a using:

using (var storage = new SqlAzManStorage(this.storageConnectionString))

Do you have any other clues?

Mar 20, 2012 at 11:59 AM

Try to add “MultipleActiveResultSets=True;” in your SQL connection string.

Mar 20, 2012 at 8:48 PM


It could be an issue in GetDBUsersEx method of NetSqlAzManStorageDataContext. The SqlDataAdapter is not wrapped up in a using statement, therefor it's not "disposed", which causes its DataReader not closed. I can fix it tonight.

Let me know your thoughts.


Mar 20, 2012 at 10:10 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Mar 20, 2012 at 10:11 PM

I have created and assigned you the work item.

Many thanks All.