Forum Moderators: open

Message Too Old, No Replies

closing a recordset that was never opened

Need some help...

         

dataguy

11:31 am on Aug 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm scripting database-intensive pages in classic ASP.

Sometimes, due to the flow of the logic on the page, there are SQL commands that are stored in a recordset, and sometimes there are just SQL commands that are executed without creating a recordset.

The problem is that I like to close and destoy the recordset at the bottom of the page using a command like:
rsInfo.Close
rsInfo = Nothing

This returns an error if the recordset was never used within the page.

It doesn't seem right to close the recordset after each use, but that is the only way I can imagine only trying to close it when it has been used.

Does this make sense? Maybe I shouldn't worry about closing it. Any help is appreciated.

txbakers

11:45 am on Aug 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if it's never opened then you don't have to close it.

dataguy

12:23 pm on Aug 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the page never uses a recordset then of course I wouldn't need to close it, but many pages may or may not use a recordset depending on external factors that effect the logic flow of the code. On those pages I need to be able to determine at the end of the code if the recordset needs to be closed or not.

Typically I close the recordset and the database connection at the end of the page. If the page sometimes uses the recordset, and sometimes it doesn't what do I do with it?

What I'm looking for is something like:

If rsExists(rsInfo) Then
rsInfo.Close
rsInfo = Nothing
End If

Otherwise I run the risk of trying to close it when it's not been used. Make sense?

mrMister

12:43 pm on Aug 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If Not (rsInfo Is Nothing) Then
if rsInfo.state = &H00000001 Then rsInfo.close()
Set rsInfo = Nothing
End If

dataguy

8:56 pm on Aug 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks mrMister, but I get an 'Object Required' error on the line 'If Not (rsInfo Is Nothing) Then' if the recordset hasn't been set.

Does what I'm trying to do make sense or should I be going at this from a different angle?

mattglet

3:03 pm on Aug 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If IsObject(rsInfo) Then
If Not (rsInfo Is Nothing) Then
if rsInfo.state <> 0 Then
rsInfo.close()
Set rsInfo = Nothing
End If
End If

aspdaddy

5:34 pm on Aug 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Simple way to do this is to have a close sub that uses on error resume next.

Public sub closeRS( objRS )
on error resume next
objRS.Close
set objRS=Nothing
end sub

dataguy

6:45 pm on Aug 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks... both the these examples work well.

mrMister

2:35 am on Aug 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If they both work then use mattglets, On Error imposes a performance hit.

aspdaddy

8:07 am on Aug 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>On Error imposes a performance hit

I have never heard that one before, can you expand a little. How does this that "hit" compare to the hit imposed by FOUR evaluations in the other example?

If it is expensive performance wise then I'd be intersted to know how expensive. The only thing IMO to watch out for with on error resume next, is that its not used at global level.

mrMister

1:28 pm on Aug 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I think I'm confusing On Error Resume Next with throwing exceptions in .Net. Thanks for picking up on that one.