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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:09:25+00:00 2026-06-04T10:09:25+00:00

Should I use using? Private Sub btntest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

  • 0

Should I use using?

 Private Sub btntest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntest.Click
        If sqlConnection.State = ConnectionState.Closed Then
            sqlConnection.Open()
        End If
        Dim query = "Select * from tablebusiness"

        Dim cmd = New MySqlCommand(query, sqlConnection)

        Dim data = cmd.ExecuteReader()

        Do While data.Read
        Loop

        Dim cmd1 = New MySqlCommand(query, sqlConnection)
    Dim data1 = cmd1.ExecuteReader //Error. Already have data reader 
                                  //Error There is already an open DataReader associated with this Connection which must be closed first.

    Dim check = 1

    'sqlConnection.Close()
End Sub
  • 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-04T10:09:27+00:00Added an answer on June 4, 2026 at 10:09 am

    Although you have not let us know what the error is (which makes solving any problem much harder), I expect the issue is arising because you are trying to re-use the SqlConnection object for 2 different commands. Especially since you are not disposing your first command before initialising the second.

    Firstly, use 2 different SqlConnection objects to manage the connection to the database. You are not putting any more overhead on the database or the code if you do this. Let the .NET framework connection pooling do its job – don’t try to do it yourself. You don’t need to do anything specific to enable connection pooling (although you can disable it by setting Pooling=false in your connection string).

    Secondly use the using statement to correctly dispose your SqlConnection, SqlCommand, and SqlDataReader objects. e.g.

        Using connection As New SqlConnection(connectionString)
            connection.Open()
            Using Command As New SqlCommand(query, connection)
                Using reader As SqlDataReader = Command.ExecuteReader()
                    While reader.Read()
                        'Do Stuff'
                    End While
                End Using
            End Using
            connection.Close()
        End Using
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the necessity of using Application.DoEvents and when we should use it?
I have been using Jquery alot lately an was wondering if I should use
I have two questions. 1) Should you always use a using statement on a
If I'm alreadu using jquery library on site then should i use origional sifr
Using WebRequest How to POST things, Should I use GetRequestStream? and how to format
I am trying to realize a use-case using git. Use Case : It should
When exposing some code to D-Bus using Qt D-Bus bindings, when should one use
I can't use long long; what should I be using?
I've read I should use money , but in todays fast paced world maybe
Why sometimes I should use block and other times &block inside functions that accept

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.