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

  • SEARCH
  • Home
  • 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 8296525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:03:06+00:00 2026-06-08T15:03:06+00:00

After executing the Command using SqlDataAdapter, connection with the db is not getting closed.

  • 0

After executing the Command using SqlDataAdapter, connection with the db is not getting closed. Let me know what needs to be done. Here is the code snippet

 DataSet dsResult = new DataSet("Result");
            SqlCommand selectCommand = new SqlCommand();
            if (_datasource.DataType == DataType.SqlText)
            {
                selectCommand = GenerateCommand(_datasource.DataType,_sqlquery);
            }
            else
            {
                selectCommand = GenerateCommand(_datasource.DataType, _datasource.DataObjectName, _fieldNames, _filters);
            }

            SqlDataAdapter da = new SqlDataAdapter(selectCommand.CommandText, _datasource.ConnectionString);
            da.Fill(dsResult);



            dataset = dsResult;

Tried explicity closing the connection like da.SelectCommand.Connection.Close(). But issue did not get fixed.
Also tried the following still issue not fixed

using(SqlDataAdapter da = new SqlDataAdapter(selectCommand.CommandText, _datasource.ConnectionString))
{
                da.Fill(dsResult);
}

Let me know what can be done to release the session.

  • 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-08T15:03:09+00:00Added an answer on June 8, 2026 at 3:03 pm

    The Fill method retrieves rows from the data source using the SELECT
    statement specified by an associated SelectCommand property. The
    connection object associated with the SELECT statement must be valid,
    but it does not need to be open. If the connection is closed before
    Fill is called, it is opened to retrieve data, then closed. If the
    connection is open before Fill is called, it remains open.

    http://msdn.microsoft.com/en-us/library/377a8x4t.aspx

    I highlighted you case. You haven’t opened the connection, so the DataAdapter will open it automatically for you and close it when it’s finished.

    Edit: If you want to manage the connection yourself, you should always close it immediately you’re finished with it.

    Therefor you could use the using-statement which disposes/closes it (even in case of an exception).

    using(var con = new SqlConnection(_datasource.ConnectionString))
    {
        using(var da = new SqlDataAdapter(selectCommand.CommandText, con))
        {
            con.Open(); // not needed but ...
            da.Fill(dsResult); // will not close the conection now
        }
    } // will close the connection
    

    Edit2: Closing a conection does not mean that it is closed physically. It is just a hint for the Connection-Pool that it can be used again.

    ExecuteReader requires an open and available Connection. The connection's current state is Connecting

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

Sidebar

Related Questions

I am getting Incorrect syntax near the keyword 'select' after executing the following code.
I am getting some problem while executing binary executable file using Java code. My
After establishing a remote connection to a MySQL server (using the MySQL command-line front-end)
Why after executing the command openssl sha1 < /dev/null | wc --bytes the output
I have an awesome trouble with Gem. After executing this command: rm -f /usr/local/lib/ruby/gems/1.9.1/cache/*
This class hangs at Future.get() method after executing ExecutorService.shutdownNow() called. I don't know what
I connect to a mysql database using pymysql and after executing a request I
we are using maven bases selenium project to test the GUI. After executing the
I'm wondering what would be the correct approach after executing a command that allocates
In my program I'm executing given command and getting result (log, and exit status).

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.