I am using the save/load grid state feature in JS for my Kendo UI MVC Grid. It looks like this:
Saving state:
var grid = $("#Items").data("kendoGrid");
sessionStorage.GridState = kendo.stringify(grid.getOptions());
Loading state:
$(document).ready(function () {
var grid = $("#Items").data("kendoGrid");
var originalOptions = grid.getOptions();
var stringifiedOptions = sessionStorage.GridState;
if (stringifiedOptions) {
console.log("Loaded Grid State.");
var loadedOptions = JSON.parse(stringifiedOptions);
loadedOptions.columns = originalOptions.columns;
grid.setOptions(loadedOptions);
sessionStorage.removeItem("GridState");
console.log("Clearing Grid State Storage.");
}
});
The first time the grid loads, grid state gets saved after clicking the edit button, which takes me to another page, but then if I click back from this edit page to go back to the grid page, it shows this error:
{"There is already an open DataReader associated with this Command which must be closed first."}
dataSourceResult.Total = source1.Count();
And I can see the load grid ajax action is being called twice, and it errors out on the second time. The error occurs when I call MyBaseEntitiesQueryable.ToDataSourceResult(request). I can't debug the Kendo UI code, but when I was able to copy the classes in the grid filtering to mimic it and it occurs on this line in ToDataSourceResult:
dataSourceResult.Total = source1.Count();
The full error and stack trace:
{"There is already an open DataReader associated with this Command which must be closed first."}
at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(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, TaskCompletionSource`1 completion, Int32 timeout, Task& 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.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
{"An error occurred while executing the command definition. See the inner exception for details."}
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at Kendo.Mvc.Extensions.QueryableExtensions.Count(IQueryable source)
at Kendo.Mvc.Extensions.QueryableExtensions.CreateDataSourceResult[TModel,TResult](IQueryable queryable, DataSourceRequest request, ModelStateDictionary modelState, Func`2 selector)
at Kendo.Mvc.Extensions.QueryableExtensions.ToDataSourceResult(IQueryable queryable, DataSourceRequest request, ModelStateDictionary modelState)
at Kendo.Mvc.Extensions.QueryableExtensions.ToDataSourceResult(IQueryable enumerable, DataSourceRequest request)
at Sandbox.Controllers.EntityController`5.LoadGrid(DataSourceRequest request) in C:\Users\shol726\Documents\Visual Studio 2015\Projects\Drs\samples\Sandbox\Controllers\EntityController.cs:line 75
at lambda_method(Closure , Object , Object[] )
at Microsoft.AspNetCore.Mvc.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionFilterAsync>d__28.MoveNext()
To make matters worse, this error happens only about 75% of the time, not all the time. Sometimes I get "The underlying provider failed to Open." as well. But that is much rarer.
How can I fix this?