Asked by:
There is already an open DataReader associated with this Command which must be closed first.

Question
-
User-997568385 posted
I'm getting the "There is already an open DataReader associated with this Command which must be closed first." message when I publish my site to Azure. It works fine when I run it locally and I add MultipleActiveResultSets=true; to the connectionString.
My code:
<%@ Page Title="User Maintenance" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Ch20UserMaintenance.Default" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="mainPlaceholder" runat="server"> <h1>Maintain users and roles</h1> (Only users associated with the Admin role can access this page) <div class="row"> <div class="col-sm-12 table-responsive"> <h2>Users</h2> <asp:GridView ID="grdUsers" runat="server" DataKeyNames="Id" AutoGenerateColumns="false" SelectMethod="grdUsers_GetData" ItemType="Ch20UserMaintenance.Models.ApplicationUser" CssClass="table table-bordered table-striped table-condensed" OnPreRender="GridView_PreRender"> <Columns> <asp:BoundField HeaderText="User Name" DataField="UserName" /> <asp:BoundField HeaderText="Email" DataField="Email" /> <asp:TemplateField HeaderText="Roles"> <ItemTemplate> <asp:Label runat="server" Text='<%# ListRoles(Item.Roles) %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowSelectButton="true" /> </Columns> </asp:GridView> </div> <div class="col-sm-6"> <asp:DetailsView ID="dvUsers" runat="server" DataKeyNames="Id" AutoGenerateRows="false" CssClass="table table-bordered table-condensed" SelectMethod="dvUsers_GetItem" UpdateMethod="dvUsers_UpdateItem" InsertMethod="dvUsers_InsertItem" DeleteMethod="dvUsers_DeleteItem"> <Fields> <asp:BoundField HeaderText="Email" DataField="Email" /> <asp:CommandField ShowEditButton="true" ShowInsertButton="true" ShowDeleteButton="true" /> </Fields> </asp:DetailsView> </div> </div> <div class="row"> <div class="col-sm-6"> <h2>Roles</h2> <asp:GridView ID="grdRoles" runat="server" DataKeyNames="Id" AutoGenerateColumns="false" SelectMethod="grdRoles_GetData" CssClass="table table-bordered table-striped table-condensed" OnPreRender="GridView_PreRender"> <Columns> <asp:BoundField HeaderText="Role Name" DataField="Name" /> <asp:CommandField ShowSelectButton="true" /> </Columns> </asp:GridView> <asp:DetailsView ID="dvRoles" runat="server" DataKeyNames="Id" AutoGenerateRows="false" CssClass="table table-bordered table-condensed" SelectMethod="dvRoles_GetItem" UpdateMethod="dvRoles_UpdateItem" InsertMethod="dvRoles_InsertItem" DeleteMethod="dvRoles_DeleteItem"> <Fields> <asp:BoundField HeaderText="Role Name" DataField="Name" /> <asp:CommandField ShowEditButton="true" ShowInsertButton="true" ShowDeleteButton="true" /> </Fields> </asp:DetailsView> </div> <div class="col-sm-6"> <h2>Add Roles to User</h2> <div class="form-group"> <label class="control-label">Select a user:</label> <asp:DropDownList ID="ddlUsers" runat="server" SelectMethod="grdUsers_GetData" DataValueField="Id" DataTextField="UserName" CssClass="form-control"> </asp:DropDownList> </div> <div class="form-group"> <label class="control-label">Add one or more roles:</label> <asp:ListBox ID="lstRoles" runat="server" SelectionMode="Multiple" SelectMethod="grdRoles_GetData" DataValueField="Id" DataTextField="Name" CssClass="form-control"></asp:ListBox> </div> <div class="form-group"> <asp:Button ID="btnAddRoles" runat="server" Text="Add Roles" CssClass="btn btn-default" OnClick="btnAddRoles_Click" /> </div> </div> </div> </asp:Content> and the aspx.cs <%@ Page Title="User Maintenance" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Ch20UserMaintenance.Default" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="mainPlaceholder" runat="server"> <h1>Maintain users and roles</h1> (Only users associated with the Admin role can access this page) <div class="row"> <div class="col-sm-12 table-responsive"> <h2>Users</h2> <asp:GridView ID="grdUsers" runat="server" DataKeyNames="Id" AutoGenerateColumns="false" SelectMethod="grdUsers_GetData" ItemType="Ch20UserMaintenance.Models.ApplicationUser" CssClass="table table-bordered table-striped table-condensed" OnPreRender="GridView_PreRender"> <Columns> <asp:BoundField HeaderText="User Name" DataField="UserName" /> <asp:BoundField HeaderText="Email" DataField="Email" /> <asp:TemplateField HeaderText="Roles"> <ItemTemplate> <asp:Label runat="server" Text='<%# ListRoles(Item.Roles) %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowSelectButton="true" /> </Columns> </asp:GridView> </div> <div class="col-sm-6"> <asp:DetailsView ID="dvUsers" runat="server" DataKeyNames="Id" AutoGenerateRows="false" CssClass="table table-bordered table-condensed" SelectMethod="dvUsers_GetItem" UpdateMethod="dvUsers_UpdateItem" InsertMethod="dvUsers_InsertItem" DeleteMethod="dvUsers_DeleteItem"> <Fields> <asp:BoundField HeaderText="Email" DataField="Email" /> <asp:CommandField ShowEditButton="true" ShowInsertButton="true" ShowDeleteButton="true" /> </Fields> </asp:DetailsView> </div> </div> <div class="row"> <div class="col-sm-6"> <h2>Roles</h2> <asp:GridView ID="grdRoles" runat="server" DataKeyNames="Id" AutoGenerateColumns="false" SelectMethod="grdRoles_GetData" CssClass="table table-bordered table-striped table-condensed" OnPreRender="GridView_PreRender"> <Columns> <asp:BoundField HeaderText="Role Name" DataField="Name" /> <asp:CommandField ShowSelectButton="true" /> </Columns> </asp:GridView> <asp:DetailsView ID="dvRoles" runat="server" DataKeyNames="Id" AutoGenerateRows="false" CssClass="table table-bordered table-condensed" SelectMethod="dvRoles_GetItem" UpdateMethod="dvRoles_UpdateItem" InsertMethod="dvRoles_InsertItem" DeleteMethod="dvRoles_DeleteItem"> <Fields> <asp:BoundField HeaderText="Role Name" DataField="Name" /> <asp:CommandField ShowEditButton="true" ShowInsertButton="true" ShowDeleteButton="true" /> </Fields> </asp:DetailsView> </div> <div class="col-sm-6"> <h2>Add Roles to User</h2> <div class="form-group"> <label class="control-label">Select a user:</label> <asp:DropDownList ID="ddlUsers" runat="server" SelectMethod="grdUsers_GetData" DataValueField="Id" DataTextField="UserName" CssClass="form-control"> </asp:DropDownList> </div> <div class="form-group"> <label class="control-label">Add one or more roles:</label> <asp:ListBox ID="lstRoles" runat="server" SelectionMode="Multiple" SelectMethod="grdRoles_GetData" DataValueField="Id" DataTextField="Name" CssClass="form-control"></asp:ListBox> </div> <div class="form-group"> <asp:Button ID="btnAddRoles" runat="server" Text="Add Roles" CssClass="btn btn-default" OnClick="btnAddRoles_Click" /> </div> </div> </div> </asp:Content>
The stack trace:
[InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.] System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) +1544289 System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) +89 System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +268 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +118 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +64 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +161 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +12 System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c) +9 System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +72 System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) +356 System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior) +166 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +12 System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +37 [EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.] System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +112 System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues) +744 System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__6() +97 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +288 System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__5() +154 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +189 System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +279 System.Data.Entity.Core.Objects.ObjectQuery`1.Execute(MergeOption mergeOption) +31 System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption) +133 System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption) +30 System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad() +333 System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty(TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject) +86 System.Data.Entity.Core.Objects.Internal.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item) +106 System.Data.Entity.DynamicProxies.ApplicationUser_B1694AF32B388D63B010B92720D87D303814569EC09D880B08E1F70DD7BFD6E6.get_Roles() +23 ASP.maintenance_default_aspx.__DataBinding__control7(Object sender, EventArgs e) +98 System.Web.UI.Control.OnDataBinding(EventArgs e) +84 System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +150 System.Web.UI.Control.DataBind() +17 System.Web.UI.Control.DataBindChildren() +185 System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +160 System.Web.UI.Control.DataBind() +17 System.Web.UI.Control.DataBindChildren() +185 System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +160 System.Web.UI.Control.DataBind() +17 System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +167 System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +2940 System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +67 System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +34 System.Web.UI.WebControls.ModelDataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +80 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74 System.Web.UI.WebControls.GridView.DataBind() +9 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +114 System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75 System.Web.UI.Control.EnsureChildControls() +92 System.Web.UI.Control.PreRenderRecursiveInternal() +42 System.Web.UI.Control.PreRenderRecursiveInternal() +160 System.Web.UI.Control.PreRenderRecursiveInternal() +160 System.Web.UI.Control.PreRenderRecursiveInternal() +160 System.Web.UI.Control.PreRenderRecursiveInternal() +160 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +883
Thanks,
Tuesday, January 10, 2017 2:43 AM
All replies
-
User-997568385 posted
In the code above the error occurs at the
<asp:Label runat="server" Text='<%# ListRoles(Item.Roles) %>'></asp:Label>
The ListRoles function in the code behind is:
public string ListRoles(ICollection<IdentityUserRole> userRoles) { IdentityRole role; var names = new List<string>(); foreach (var ur in userRoles) { role = (from r in roleMgr.Roles where r.Id == ur.RoleId select r).SingleOrDefault(); names.Add(role.Name); } return string.Join(", ", names); }
and the role = is causing the error. Is there some better way to get a list of userRoles? Why does this work on the local but not on Azure?
Wednesday, January 25, 2017 8:09 PM