p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

proasp_codeclinic thread: To Close or Not To Close


Message #1 by mdehnel@a... on Tue, 26 Jun 2001 05:00:31
Better is something like:

<%
Sub subADOClose( _
    ByRef objToClose _
    )

    On Error Resume Next

    If objToClose.State = adStateOpen then
        objToClose.Close
    End If

    If isObject(objToClose) then
        Set objToClose = Nothing
    End If

End Sub
'
'
Call subADOClose(objRS)
Call subADOClose(objCommand)
Call subADOClose(objConn)
%>

This was you can reuse the function no matter how many recordset, command,
record or connection objects you have - you could theoretically use it for
any ADO object - as well, you aren't limited to having your recordsets
called rsShow etc

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
----- Original Message -----
From: <Thomas@m...>
To: "Code Clinic" <proasp_codeclinic@p...>
Sent: Wednesday, June 27, 2001 10:31 AM
Subject: [proasp_codeclinic] Re: To Close or Not To Close


: My experience is that adding a state check on both the recordset and
: connection before I close them. This works for me:
:
: Function closeConnection ()
:  if rsShow.state = 1 then rsShow.close
:  Set rsShow = Nothing
:  if objDBConn.state = 1 then objDBConn.Close
:  Set objDBConn = Nothing
: End Function
:
: This should close the recordset and connection inly when they're open,
: thus preventing any errors.
:
: Thomas
:
:
: > I don?t understand why this worked
: >
: > In my searches I came across these two posts:
: >
: > POST 1:
: > I keep getting an error when I try to modify a record in my database.
: The
: > update, delete, or add all work; the changes get made, however, I still
: get
: > this error:
: > " ADODB.Recordset error '800a0e78'
: >
: > The operation requested by the application is not allowed if the object
: is
: > closed.
: >
: > /preload.asp, line 47 "
: >
: > And here are the functions:
: > Function openConnection ()
: > Set objDBConn = Server.CreateObject("ADODB.Connection")
: > strConn = "[connection string]"
: > objDBConn.Open strConn
: > Set rsShow = Server.CreateObject("ADODB.Recordset")
: > rsShow.ActiveConnection = objDBConn
: > End Function
: >
: > Function closeConnection ()
: > rsShow.close
: > Set rsShow = Nothing
: > objDBConn.Close
: > Set objDBConn = Nothing
: > End Function
: >
: > POST 2:
: > Andrew,
: >
: > I have a page that does that too, I think the recordset is open, when it
: > isn't.  What I did was just deleted the lines referring to close the RS
: and
: > setting it to nothing.  Must have something to do with not explicitly
: > opening a recordset .. someone else can probably provide a more correct
: > answer based on the code you sent.  (hint to the gurus that aren't in
: > Austin.)
: >
: > *shrug*
: >
: > Michele
: >
: > This got me thinking about my code and sure enough the database was
: being
: > updated even though I was getting the error.  So the error was something
: > after the AddNew.  Than after reading Michele?s comment I realized that
: > commenting out the close statement made sense from what the error was
: > stating.  After all you can't close something that is already closed.
: So
: > WHAT is implicitly closing the recordset?  The objRS.Update command?
:
: ---
: * Fast, Full-Featured Microsoft® Excel Web Reports & Charts!
: A breakthrough in high performance Web application development,
SoftArtisans
: ExcelWriter 1.1 supports native Excel charting, image insertion, and
: advanced functions & formatting. One click generates presentation-quality
: Excel spreadsheets-and ExcelWriter performs over 100 times faster than the
: Excel Object. Several editions, including ExcelWriterFREE, are available.
: http://www.softartisans.com/softartisans/excelwriter.html>
:
$subst('Email.Unsub')


  Return to Index