Solved

VB.net How to pass Data Reader between class and Form

Posted on 2016-09-08
4
11 Views
Last Modified: 2016-10-28
So here is my situation, i am trying to stick more and more reusable code into seperate class. In this instance i am calling a function which expects a dataReader to process the data. The actual call is in my sqlHelper class.

SqlHelper Code

    Public Shared Function getNenaRecords(ByVal myStatus As String) As SqlDataReader
        Dim myCon As SqlConnection = SqlConnectString()
        myCon.Open()

        Dim Myreader As SqlDataReader

        Dim cmd As SqlCommand = New SqlCommand("sp_E911Records_byStatus_SEL", myCon)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@status", myStatus)   'Status

        Myreader = cmd.ExecuteReader()

 
        myCon.Close()
        myCon.Dispose()

        Return Myreader


    End Function

Open in new window


And i call the function from my form via

 Dim myReader2 As SqlDataReader = getNenaRecords("v")

Open in new window


When i trace my code i see that myreader has Data but myReader2 is an empty reader after the code has been processed
so not sure what i am missing here
0
Comment
Question by:AlexPonnath
  • 2
4 Comments
 
LVL 22

Assisted Solution

by:Snarf0001
Snarf0001 earned 500 total points
ID: 41790292
A datareader is a live connection to the database, which streams the records as they come out.
When you close the underlying connection, the reader is also closed.

I would suggest either:
1) Constructing class objects and passing back with a  yield statement for the underlying records
2) Using a dataset or datatable instead of the datareader.
0
 

Author Comment

by:AlexPonnath
ID: 41790323
I figured that out, if i do not close my connection the data can be passed along. Since it is in a separate class, how does vb handle this connection ? Will it automatically close it after the code has been executed or will it hang around ?
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 41790645
Eventually it will close when GC kicks in, but DB connections are one thing you definitely don't want to depend on that for.

.ExecuteReader also has an override for "CommandBehavior", if you pass in .CloseConnection, you can dispose of the datareader in your calling class, and that will terminate the connection.

Quick and "pretty safe" way to handle the issue without doing a larger code change.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now