[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


When to close a datareader and connection?

Posted on 2008-11-14
Medium Priority
Last Modified: 2012-05-05
I have the following code below in a shared function within a self-created namespace. My question is; when and where should I close the datareader and connection. I am assuming that in this example if I close the datareader after 'End While' it will be too late as the the function will have already executed its return of X.

I am assuming the same is true if I close the datareader after the 'End If'

Or am I completely wrong? and if so... where do I close the datareader/connection.
If oDR.HasRows = True Then
                    While oDR.Read
                        Return X
                    End While
                    Return 0
                End If

Open in new window

Question by:footballencounters

Accepted Solution

zkeown earned 2000 total points
ID: 22959331
You're correct, it would be too late.  Consider a variable to hold your return value, whether it is X or 0, and return it after the End If after you've closed the DataReader.

Also, if you're only going to return the one value, you don't need a while look.  If oDR.Read() then would accomplish the same.

Expert Comment

ID: 22959346
instead of returning x in the loop assign it to a variable.

the code will then exit out the loop and this is where you close your connection / reader.

the code will then exit the if statment and its here where you should return the value

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month19 days, 10 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question