Closing db connection and redirecting in a loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luis

    Closing db connection and redirecting in a loop

    set RS = conn.Execute("S ELECT * FROM TblName")
    do while not RS.eof
    if X = "Blah" then
    'do something
    RS.close
    set RS = nothing
    response.redire ct "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.redir ect "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.redire ct 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?
  • Kindler Chase

    #2
    Re: Closing db connection and redirecting in a loop

    Luis wrote:[color=blue]
    > set RS = conn.Execute("S ELECT * FROM TblName")
    > do while not RS.eof
    > if X = "Blah" then
    > 'do something
    > RS.close
    > set RS = nothing
    > response.redire ct "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.redir ect "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.redire ct 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?[/color]

    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("S ELECT * 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.redire ct "report.asp "



    --

    kindler chase

    Home of SuperInvoice's Fortress of BeanCounters

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


    Comment

    • Kindler Chase

      #3
      Re: Closing db connection and redirecting in a loop

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

      Dim blnFound : blnFound = False

      set RS = conn.Execute("S ELECT * 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

      Home of SuperInvoice's Groove Thang

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


      Comment

      Working...