Passing SQLDataReader through a function

J

James P.

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
 
F

Fergus Cooney

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
 
F

Fergus Cooney

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
 
J

James P.

Fergus Cooney said:
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
 
F

Fergus Cooney

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
 
J

James Pham

Fergus,

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

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top