473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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.NullReferenceException' 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 = getStoreProcedures("StoreProcedure")
End Function
Public Function getStoreProcedures() As SqlDataReader
Dim sp As SqlDataReader =
CCMSDB.DBDataReader("procSelectBatchReports")
'This sp.Read returns rows displayed in the message box fine.
While sp.Read
MsgBox(sp("StoreProcedure"))
End While
'This code works fine until here
getStoreProcedures = 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 9009
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 getStoreProcedures takes a parameter.
Dim xp As SqlDataReader = getStoreProcedures("StoreProcedure")

The other doesn't
Public Function getStoreProcedures() 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 getstoreproceduces=sp

"James P." <jp***@sandsexpo.com> wrote in message
news:bd**************************@posting.google.c om...
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.NullReferenceException' 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 = getStoreProcedures("StoreProcedure")
End Function
Public Function getStoreProcedures() As SqlDataReader
Dim sp As SqlDataReader =
CCMSDB.DBDataReader("procSelectBatchReports")
'This sp.Read returns rows displayed in the message box fine.
While sp.Read
MsgBox(sp("StoreProcedure"))
End While
'This code works fine until here
getStoreProcedures = 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**************@TK2MSFTNGP10.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 getStoreProcedures takes a parameter.
Dim xp As SqlDataReader = getStoreProcedures("StoreProcedure")

The other doesn't
Public Function getStoreProcedures() 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
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...
4
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...
5
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...
2
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...
2
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
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...
4
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...
3
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...
2
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.