Closing db connection and redirecting in a loop

Discussion in 'ASP General' started by Luis, Mar 1, 2004.

  1. Luis

    Luis Guest

    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?
     
    Luis, Mar 1, 2004
    #1
    1. Advertising

  2. Luis wrote:
    > 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
     
    Kindler Chase, Mar 1, 2004
    #2
    1. Advertising

  3. 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
     
    Kindler Chase, Mar 1, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    661
    Thomas Hansen
    Feb 15, 2007
  2. johnny
    Replies:
    1
    Views:
    803
    Jerry Hill
    Dec 8, 2006
  3. shmuel witman

    Connection timeout (connection closing after 100 sec )

    shmuel witman, Dec 4, 2005, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    848
    shmuel witman
    Dec 6, 2005
  4. thomas
    Replies:
    0
    Views:
    236
    thomas
    Oct 23, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    398
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page