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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:59:55+00:00 2026-06-13T14:59:55+00:00

I have an ASP.Net 2.0 Web Forms Application using SQL Server 2008. The application

  • 0

I have an ASP.Net 2.0 Web Forms Application using SQL Server 2008. The application has a UI layer and Data Access Layer. I use Enterprise Libray 5.0 to persist the data.

Recently my site has been running very slow, especially on pages where there are maybe 15-20 separate reads from the database. I am very concerned that my SqlDataReader database connections are not being closed properly. Below is an example of how code works. Please take a look and let me know if you see any issues with it in terms of leaking connections.

Data Access Class

Public Class DataAccess

    Private db As Database = DatabaseFactory.CreateDatabase()

    Public Function ExecuteDataReader(ByVal params() As SqlParameter, ByVal SProc As String) As SqlDataReader

        Dim i As Integer
        Dim dr As SqlDataReader = Nothing
        Dim cmd As DbCommand

        cmd = db.GetStoredProcCommand(SProc)
        cmd.CommandTimeout = 120

        For i = 0 To params.Length - 1
            db.AddInParameter(cmd, params(i).ParameterName.ToString, params(i).DbType, params(i).Value)
        Next

        dr = TryCast(DirectCast(db.ExecuteReader(cmd), RefCountingDataReader).InnerReader, SqlDataReader)

        Return dr

    End Function

UI Code Behind Page

Dim drSource As SqlDataReader = Nothing
        Try
            Dim params(0) As SqlParameter
            params(0) = New SqlParameter("@applicant_id", Session("ApplicantID"))
            drSource = DataAccess.ExecuteDataReader(params, "sp_get_date_last_login")
            If drSource.HasRows Then
                drSource.Read()
                'Do code
            End If
        Finally
            If Not (drSource Is Nothing) Then
                drSource.Close()
            End If
        End Try

I tried to put the code below into my ExecuteDataReader method, but this then closes the SqlDataReader before it gets a chance to do a Read

if (cmd.Connection.State == ConnectionState.Open)
            cmd.Connection.Close();

Can someone please look at the code above and let me know how to properly close my database connections, or maybe I am already doing it?

Thanks for your help folks.

  • 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-13T14:59:56+00:00Added an answer on June 13, 2026 at 2:59 pm

    Have you tried getting the underlying call to ExecuteReader to take the CommandBehavior.CloseConnection parameter? At least this will ensure that the connection is closed when the DataReader is also closed. This will rely on consumers of the DataReader passed back from ExecuteDataReader() to close it explicitly or dispose via a Using block.

    Alternatively, try adding the following to the code behind after the drSource.Close() line:

     drSource.Connection.Close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My ASP.NET application is using SQL Server 2008 backend. I have a Web Form
I have a ASP.NET Web Forms application and I'm using some dynamic controls in
I have an ASP.NET web application that is using forms authentication. Everything is configured
We have been using log4net for logging our asp.net web forms application. Our logging
My team and I have a asp.net web forms application and are using several
I have an ASP .NET Web Forms application that makes use of the 'WebMethod'
I have a asp.net web forms app that uses System.Web.Caching.Cache to cache xml data
I have an ASP.NET Web Forms web site. It has a page inside of
I have an ASP.NET 4 Web Forms app that is using URL Routing. I
I have a pretty big web application that I created last year using ASP.NET

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.