Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multiple instances of executereader and an open connection

Posted on 2006-06-11
2
Medium Priority
?
626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
fullcontact earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

618 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