Go Premium for a chance to win a PS4. Enter to Win


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
  • 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.

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.


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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

926 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