Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:50:36+00:00 2026-06-02T09:50:36+00:00

I created a connection and an SqlReader but forgot to close both of them:

  • 0

I created a connection and an SqlReader but forgot to close both of them:

SqlConnection conn = new SqlConnection("connection info");
conn.Open();

string sql = "SQL command HERE";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();

Now when try to run the code again it always gives me this error:

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

This link told me how to properly open and close a connection but didn’t explain anything on how to close one still running.

I tried shuting down the pc, I tried looking into the database’s options on SQL server (found none useful)… I changed the code to do just the close of both the connection and the reader (it compiled and runned but the problem remained after changing back the code).

How can I close this “ghost” connection? Is there any way (brute force) to close all running connections?


[EDIT:] I couldn’t really solve the problem. The workaround was to add MultipleActiveResultSets=true to the connection string

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:50:38+00:00Added an answer on June 2, 2026 at 9:50 am

    Looking at all the answers, they seem to tell you how to avoid the problem.

    If I’m not mistaken, what you mean is that a connection exists on both the client (your PC) and the server (The sql server) because you forgot to close it, and you’re worried about it hanging out there forever.

    Think of your connection to the server as a phone conversation. I could hang up on you, but it takes a few seconds for your phone to realize the connection is lost. You may sit there wondering if I’ve hung up, or just stopped talking. You really don’t know. This is what happens on the server when a connection isn’t closed properly. (On older landlines, you could leave the phone off the hook and tied up the line indefinitely.)

    By closing the connection in code, you are effectively telling the server to close their end of the connection before closing your own. if you FAIL to close the conneciton, it will be closed on your end when the program exits or if you reboot, but the server may sit there with an open connection. (Think of someone sitting there wondering “Did he just hang up on me?”)

    If I’m not mistaken, what you want to get to is closing it at the SQL server end. (Getting them to “hang up”.)

    After rebooting, it is absolutely closed on your end. It should clear on its own at the server.

    However, if you want to do it yourself, you can clear it at the server in code end using this info: How do you kill all current connections to a SQL Server 2005 database?

    A far easier approach would be to just do it in SQL Server Management Studio as described here: http://www.mikebevers.be/blog/2009/07/kill-open-sql-processes-connections/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to set my connection string for Linq to Sql based on an
Yesterday I created a new project in Codeplex. I created a connection from Visual
I recently found an automatically created connection string specifying Pooling=False and wondered as to
c#.Net 3.5 with a SQL Server 2000 backend, I have a connection string in
I have an active SMO connection string server = textBox_Server.Text; ServerConnection srvConn = new
In Jdbc,the statement was created after successfully created connection then we are called st=conn.createStatement()
How to set compression method for SSL/TLS connection created with M2Crypto and TwistedProtocolWrapper
I have created a class that will manage connection and commands, I named it
I have created a service class for my network connection so that my app
On the class level, I have created reference: System::Data::Odbc::OdbcConnection Connection; in some method I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.