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 5843727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:11:41+00:00 2026-05-22T12:11:41+00:00

I have written web application and while testing it I found that the connections

  • 0

I have written web application and while testing it I found that the connections established with the server are not being closed even though I close the connection in the application. Even after the web page is closed, the connection remains as it is.
Here is a sample code snippet which opens a connection and closes it:

protected void OpenConnection_Click(object sender, EventArgs e)
{
    SqlConnection conn = null; 
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
    builder.DataSource = "MyServerName";
    builder.InitialCatalog = "Northwnd";
    builder.IntegratedSecurity = true;
    builder.ApplicationName = "My Test ASP";

    try
    {
        conn = new SqlConnection(builder.ConnectionString);
        conn.Open();
        conn.Close();
    }
    catch (SqlException ex)
    {
        ex.Message.ToString();
    }

}

In the activity monitor the connection still remains. If I execute the same code in a normal Windows application the connection is getting closed properly.

Please help me how to resolve this issue.

  • 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-05-22T12:11:41+00:00Added an answer on May 22, 2026 at 12:11 pm

    You should use using for better managing resources. There is a big flaw in your code that if your code encounters an exception, the connection will not be closed and this will cause severe problems. Rewriting your code would result in:

    protected void OpenConnection_Click(object sender, EventArgs e)
    {
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = "MyServerName";
        builder.InitialCatalog = "Northwnd";
        builder.IntegratedSecurity = true;
        builder.ApplicationName = "My Test ASP";
    
        using (SqlConnection conn = new SqlConnection(builder.ConnectionString))
        {
        try
        {
            conn.Open();
            // Do Some stuff with SqlConnection
        }
        catch (SqlException ex)
        {
            ex.Message.ToString();
        }
      }
    }
    

    When the using block ends, it automatically calls the dispose method of the using variable which is SqlConnection here. Note that calling dispose on SqlConnection will also call its Close() method, you can investigate it in reflector.

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

Sidebar

Related Questions

I have a legacy web application that was written long before there were even
I have inherited a poorly written web application that seems to have errors when
I have a web application written in C# that consumes several internal web services.
I have an asp.net web application written in C# using a SQL Server 2008
I have a web application that was originally written on .Net 1.1 Framework. I
I have written an application that queries a web service I wrote (which returns
I have an application that is an asmx web service written in .NET 2.0
I have web application written in java using Eclipse. It has just one servlet
I have a web application written using CherryPy, which is run locally on 127.0.0.1:4321
I have a web-application written in java. I would like to run integration tests

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.