Solved

Multiple instances of executereader and an open connection

Posted on 2006-06-11
2
614 Views
Last Modified: 2013-12-25
Greetings Experts,

My question involves using an executeReader in a for/next loop.  My code: \

Dim SConn As SqlConnection = New SqlClient.SqlConnection(sConnectionString)
 SConn.Open()

 Dim cmd2 As New SqlCommand
 cmd2.Connection = SConn

  For Incr = 0 To Counter
        Str = "Select Officer from RescueCheckout Where RDate = '" & RDate & "' and RescueNumber = '" & Units(Incr) & "'"
        cmd2.CommandText = Str
        cmd2.Connection = SConn
         sqlreader = cmd2.ExecuteReader
         If sqlreader.HasRows  Then
                info(Jncr) = Units(Incr)
                Jncr = Jncr + 1
            End If
            SConn.Close()
 Next
 
It appears that I must close and then reopen a connection for each iteration of the for/next.  Is this correct?  I would think that I could use the same connection for each ExecuteReader.

Thansk

V Whitehead


   
   
0
Comment
Question by:VSWhite
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
fullcontact earned 500 total points
Comment Utility
You do not need to close your connection within to loop, but you need to close the reader. You can only have one reader open at a time, ie :-

Replace

SConn.Close

With

sqlreader.close
0
 
LVL 3

Expert Comment

by:fullcontact
Comment Utility
Could you try and use a single SQL to do this ie not using a loop 0 to counter

Select RescueNumber from RescueCheckout Where RDate = '" & RDate & "' and RescueNumber between 0 AND " & Counter

 This would return all the rescuenumbers which exists between 0 and counter.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now