Closing db connection and redirecting in a loop

L

Luis

set RS = conn.Execute("SELECT * FROM TblName")
do while not RS.eof
if X = "Blah" then
'do something
RS.close
set RS = nothing
response.redirect "report.asp"
end if
RS.movenext
loop
RS.close
set RS = nothing
conn.close
set conn = nothing

In the above code is it necessary close the RS object and set it to
nothing before the 'response.redirect "report.asp"', or is it ok to
just close everything at the end?

The 'RS.close : set RS = nothing' code is repeated twice so that the
RS object is closed regardless of whether the condition in the If
statement is met. Is there a better way to write this so that the
'RS.close : set RS = nothing' code isn't repeated?

Should I have a response.redirect in the middle of a loop?
If the X condition in the If statement is matched then the redirect is
done while conn is still open. Surely this is "bad" coding?
 
K

Kindler Chase

Luis said:
set RS = conn.Execute("SELECT * FROM TblName")
do while not RS.eof
if X = "Blah" then
'do something
RS.close
set RS = nothing
response.redirect "report.asp"
end if
RS.movenext
loop
RS.close
set RS = nothing
conn.close
set conn = nothing

In the above code is it necessary close the RS object and set it to
nothing before the 'response.redirect "report.asp"', or is it ok to
just close everything at the end?

The 'RS.close : set RS = nothing' code is repeated twice so that the
RS object is closed regardless of whether the condition in the If
statement is met. Is there a better way to write this so that the
'RS.close : set RS = nothing' code isn't repeated?

Should I have a response.redirect in the middle of a loop?
If the X condition in the If statement is matched then the redirect is
done while conn is still open. Surely this is "bad" coding?

You've done it correct. The object isn't closed out if you redirect since
the code never made it that far.

If you really wanted to rewrite it:

Dim blnFound : blnFound = False

set RS = conn.Execute("SELECT * FROM TblName")

do while not RS.eof
if X = "Blah" then
blnFound = True
Exit Do
RS.movenext
loop

RS.close
set RS = nothing
conn.close
set conn = nothing

If blnFound Then response.redirect "report.asp"



--

kindler chase
http://www.ncubed.com
Home of SuperInvoice's Fortress of BeanCounters

news://news.ncubed.com/support
n3 Support Group
 
K

Kindler Chase

Kindler Chase wrote:
Sorry, forgot the end if :)

Dim blnFound : blnFound = False

set RS = conn.Execute("SELECT * FROM TblName")

Do While Not RS.eof
If X = "Blah" Then
blnFound = True
Exit Do
End If
RS.movenext
Loop

RS.close
set RS = nothing
conn.close
set conn = nothing

--

kindler chase
http://www.ncubed.com
Home of SuperInvoice's Groove Thang

news://news.ncubed.com/support
n3 Support Group
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top