473,749 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing SQLDataReader through a function

I'm trying to pass a sqlDataReader from a called function to a calling
function but got nothing back and a message saying "An unhandled
exception of type 'System.NullRef erenceException ' occurred in
Throw.exe. Object reference not set to an instance of an object."

Here is my code:
Public Function testint() As String
'Nothing returns here, plus the error above
Dim xp As SqlDataReader = getStoreProcedu res("StoreProce dure")
End Function
Public Function getStoreProcedu res() As SqlDataReader
Dim sp As SqlDataReader =
CCMSDB.DBDataRe ader("procSelec tBatchReports")
'This sp.Read returns rows displayed in the message box fine.
While sp.Read
MsgBox(sp("Stor eProcedure"))
End While
'This code works fine until here
getStoreProcedu res = sp
'After this, when it returns to Testint, it will blow up
end function

Can anyone help please? I'm new to VB.NET. Thanks in advance,
James
Nov 20 '05 #1
6 9040
Hi James,

There doesn't seem to be anything wrong with the code that you have shown,
except that it isn't the code that you are using!!

One getStoreProcedu res takes a parameter.
Dim xp As SqlDataReader = getStoreProcedu res("StoreProce dure")

The other doesn't
Public Function getStoreProcedu res() As SqlDataReader

This isn't the cause of the Exception, but it may be the cause of me not
being able to see what is!!

Regards,
Fergus

Nov 20 '05 #2
Mat
try
Return sp
instead of getstoreprocedu ces=sp

"James P." <jp***@sandsexp o.com> wrote in message
news:bd******** *************** ***@posting.goo gle.com...
I'm trying to pass a sqlDataReader from a called function to a calling
function but got nothing back and a message saying "An unhandled
exception of type 'System.NullRef erenceException ' occurred in
Throw.exe. Object reference not set to an instance of an object."

Here is my code:
Public Function testint() As String
'Nothing returns here, plus the error above
Dim xp As SqlDataReader = getStoreProcedu res("StoreProce dure")
End Function
Public Function getStoreProcedu res() As SqlDataReader
Dim sp As SqlDataReader =
CCMSDB.DBDataRe ader("procSelec tBatchReports")
'This sp.Read returns rows displayed in the message box fine.
While sp.Read
MsgBox(sp("Stor eProcedure"))
End While
'This code works fine until here
getStoreProcedu res = sp
'After this, when it returns to Testint, it will blow up
end function

Can anyone help please? I'm new to VB.NET. Thanks in advance,
James

Nov 20 '05 #3
Hi Mat,

For backwards compatibility (I believe), assignment to the Function name
is a legitimate way to return a value

Function Foo As String
Foo = "I'm going places!!"
End Function

Regards,
Fergus
Nov 20 '05 #4
"Fergus Cooney" <fi******@tesco .net> wrote in message news:<uD******* *******@TK2MSFT NGP10.phx.gbl>. ..
Hi James,

There doesn't seem to be anything wrong with the code that you have shown,
except that it isn't the code that you are using!!

One getStoreProcedu res takes a parameter.
Dim xp As SqlDataReader = getStoreProcedu res("StoreProce dure")

The other doesn't
Public Function getStoreProcedu res() As SqlDataReader

This isn't the cause of the Exception, but it may be the cause of me not
being able to see what is!!

Regards,
Fergus


Fergus,
Thanks a lot for pointing out my classic mistake. It works after I modified it.
James
Nov 20 '05 #5
Hi James,

Strange, I'd have expected the compiler to pick that one up.

No matter - you're back on the road again. :-)

Regards,
Fergus
Nov 20 '05 #6
Fergus,

Thanks a lot for pointing out the missing parameter in the function. It
works now.

James

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3001
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered techniques where i create a sql string in the page and pass it to the Db and retrieveing data from a stored procedure, but I can't get the hang of parameters. I have a method where I can get the parameters passed to the sp but it doesn't want to return any results. Here's a copy of my code:
4
2914
by: Michael Carr | last post by:
I have a function that populates a class with values from a database. I'd like to pass into the function either a SqlDataReader or a DataRow, depending on which mechanism I'm using to retrieve data from the database. However, the two classes don't appear to have any common interfaces that would allow me to enumerate the fields. Yet, when you databind you can pass either of these classes (as well as many others) and .NET somehow knows how...
5
1650
by: blue | last post by:
We often get connection pooling errors saying that there are no available connections in the pool. I think the problem is that we are passing around open readers all over the place. I am planning on changing this in our code and I expect this to fix our problem. We have our connection pooling set to the default number of connections open. We probably have about 3-7 users concurrently using our web site. So, the problem isn't that we...
2
1573
by: vengala s reddy | last post by:
i have login.aspx, login.vb, i have my stored procedures in login.vb in login.aspx <%@ Page Language="vb" src="/MyTime/com/login.vb" Inherits="Enter.MyTime"%> in login.vb Public Sub EnterMyTime(ByVal ID As String, ByVal PWD As String)as dataset
2
8086
by: Martin Raychev | last post by:
Hi all, I have the following problem: I have a private method that returns a SqlDataReader. For this to work I have not to close the DB connection in the above method. I do this only to
3
1999
by: Miguel | last post by:
Hi, I'm new to .NET and am using VB .NET as I am from a VB background. I am having difficulty understanding the way .NET handles, passes and uses objects. I have had a look at the Micrsoft Data Access Application blocks for .NET and am struggling to understand the following: 1. When creating a function that returns a datareader, there is a datareader instantiated inside the function. This datareader is then returned to the
4
1310
by: paapa21 | last post by:
I want to Declear a variable as SqlDataReader in ASP 2.0. But when i do so it tells me SqlDataReader is not define. This same code works in my previous application in ASP 1.0. Has this change? Can someone show me how to do this. Below is the function and the line of the error is bold. Function GetUniversity() As String Dim Unipayment As New UnipaymentDB Dim CIDR As SqlDataReader = Unipayment.StudentState(Request.Params("StudentID"))
3
4368
by: rn5a | last post by:
A SqlDataReader is populated with the records from a SQL Server 2005 DB table. The records retrieved depends upon 2 conditions (the conditions depend on what a user selects in an ASPX page). If condition1 is true, then the SqlDataReader will be populated with the records existing in table1 & will return 0 to the calling function but if condition2 is true, then the SqlDataReader will be populated with the records from another table named...
2
3604
by: Aaaaash | last post by:
I passed the Datareader as the referenced parameter, but it showing the following error. Value of type '1-dimensional array of System.Data.SqlClient.SqlDataReader' cannot be converted to 'System.Data.SqlClient.SqlDataReader'. My Code is as below Function Public Function getAllPostalCode(ByRef rsPostalCode As SqlClient.SqlDataReader) As Boolean
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.