Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Problem with multiple connections using a shared Linq Context

Posted on 2008-09-30
Medium Priority
Last Modified: 2013-11-11
I have tried to develop a site using linq to manage and process the dynamic data on the site.
When it is just a few people  accessing the site, it works fine. When we have more people accessing the site (we had 20 people testing today) we began recieving the following error:

ANY help would be greatly appreciated!

ExecuteReader requires an open and available Connection. The connection's current state is open. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is open.
Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 
Stack Trace: 
[InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is open.]
   System.Data.SqlClient.SqlConnection.GetOpenConnection(String method) +4874932
   System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) +11
   System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +144
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +87
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
   System.Data.Common.DbCommand.ExecuteReader() +12
   System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +975
   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.DataContext.ExecuteMethodCall(Object instance, MethodInfo methodInfo, Object[] parameters) +69
   DinnTrophyClasses.ConfiguratorDataContext.Site_Basket_Item_GetAwardBasketItems(Nullable`1 intBasketID, Nullable`1 intAwardID, Nullable`1 intAwardComponentID, Nullable`1 intShiptoId) in C:\Projects\DinnTrophy\Solutions\DinnTrophyClasses\Configurator.designer.vb:309
   DinnTrophyClasses.AwardConfigurator.get_AwardsForBasketByBasketId(Int32 IntBasketID) in C:\Projects\DinnTrophy\Solutions\DinnTrophyClasses\AwardConfigurator.vb:197
   CartSummary_Content.RefreshTotals() in C:\Inetpub\wwwroot\DinnTrophy\UserControls\CartSummary_Content.ascx.vb:25
   CartSummary_Content.OnPreRender(EventArgs e) in C:\Inetpub\wwwroot\DinnTrophy\UserControls\CartSummary_Content.ascx.vb:37
   System.Web.UI.Control.PreRenderRecursiveInternal() +80
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

Open in new window

Question by:tdartt
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 64

Expert Comment

by:Fernando Soto
ID: 22607487
It is stating that you can not use the ExecuteReader that is already open. It sounds like you are using the same DataContext object for multiple request.

Author Comment

ID: 22607673
Is there a way to use a Datacontext for asynchronous use? or do you need a seperate datacontext for each call? I have a web application that is accessing static information loaded into a single datacontext.
LVL 64

Expert Comment

by:Fernando Soto
ID: 22607868
Hi tdartt;

DataContext Class: From Microsoft documentation.

In general, a DataContext instance is designed to last for one  "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to  create. A typical LINQ to SQL application creates DataContext instances at method scope or as a  member of short-lived classes that represent a logical set of related database  operations.

With that said you state that, "I have a web application that is accessing static information loaded into a single datacontext." If it is static information then there is no need to re-query the database. I would convert the data into a list. Once that is done you access the list and no need to execute a new query.

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?


Author Comment

ID: 22607894
Thats an excellent idea :) I will give it a shot.
LVL 64

Accepted Solution

Fernando Soto earned 620 total points
ID: 22624497
Hi tdartt;

Have you tried the solution?


Author Comment

ID: 22626160
I ended up using Rick Strahls Data Context factory and it worked like a charm :)

Thanks for the help!
LVL 64

Expert Comment

by:Fernando Soto
ID: 22626671
Very good.

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question