Solved

Multiple instances of executereader and an open connection

Posted on 2006-06-11
2
619 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
ID: 16885340
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
ID: 16885397
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

832 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