sqlDataReader not returning records when called inside class

G

Guest

I have a funtion that works fine and dandy when called from anywhere in my
app. It will NOT work when called from inside the class in which it resides.

This is the function I'm calling: "getProductByID(productID)" from inside
another method in the same class. See below.

This line throws a null ref exception:
While dr.Read()

Thanks for any insight!



Public Sub deleteProduct(ByVal productID)

'Set up parameters
Dim arParms(0) As SqlParameter
arParms(0) = New SqlParameter("@productID",
SqlDbType.UniqueIdentifier)
arParms(0).Value = productID

'get the images to delete
Dim context As System.Web.HttpContext = System.Web.HttpContext.Current


Dim dr As SqlDataReader = getProductByID(productID)

Dim thumbImg As String
Dim fullImg As String

While dr.Read()
'delete thumb
fullImg = context.Application("webRoot") &
"vendorImages/" & dr.Item("galleryFolder").ToString & "/" &
dr.Item("imageFull").ToString
fullImg = context.Current.Server.MapPath(fullImg)
Dim thisImgFull As New System.IO.FileInfo(fullImg)
thisImgFull.Delete()
End While


.... omitted for brevity


End Sub
 
K

Ken Dopierala Jr.

Hi,

Try:

Dim dr As New SqlDataReader = getProductByID(productID)

Also make sure that productID actually holds what you want it to when you
pass it. Here:

Public Sub deleteProduct(ByVal productID)

You are not giving it a type, should it be an Integer? Good luck! Ken.
 
G

Guest

HI Ken, thanks for the reply. productID is a uniqueidentifier and it's
definitely coming in, I've double checked that. I also tried your
suggestiond, but no go. :(
 
K

Ken Dopierala Jr.

Hi,

The problem is that dr is Nothing which means that for some reason your
getProductByID() is returning nothing instead of a data reader. I would
step through the code and make sure that getProductByID() is passing back a
valid data reader when called from your delete function. Also make sure the
data reader isn't being closed or going out of scope before you pass it
back. Can you post your getProductByID() function code and also tell what
the relationship between that and the delete function are code wise? I mean
are they in the same module or different classes. We need to find out why
Nothing is return by getProductByID() when called from the delete function
and not from other code in your solution. Ken.
 
G

Guest

Here's the getProductByID function. And the two methods are in the same
class. It's definitely not returning a valid sqlDatareader, just don't know
why that is.

thanks.



Public Function getProductByID(ByVal productID) As SqlDataReader

'Set up parameters
Dim arParms(0) As SqlParameter
arParms(0) = New SqlParameter("@productID",
SqlDbType.UniqueIdentifier)
arParms(0).Value = productID
'insert the data
Dim dr As SqlDataReader = SqlHelper.ExecuteReader(connStr,
CommandType.StoredProcedure, "productByIDGet", arParms)
Return dr

End Function
 
K

Ken Dopierala Jr.

I can' tell why it isn't working. You could put a breakpoint on the Return
dr line. Then call this code from where it works in your code and also from
where it doesn't. If dr is valid at the breakpoint for one call and Nothing
for another then the problem has to be at what you are passing in. Also you
might want to put the New keyword in your Dim dr As SqlDataReader = ....
line below. Good luck! Ken.
 
G

Guest

2 pennies - is connStr actually declared and initialised ?
I have a funtion that works fine and dandy when called from anywhere in my
app. It will NOT work when called from inside the class in which it resides.

This is the function I'm calling: "getProductByID(productID)" from inside
another method in the same class. See below.

This line throws a null ref exception:
While dr.Read()

Thanks for any insight!



Public Sub deleteProduct(ByVal productID)

'Set up parameters
Dim arParms(0) As SqlParameter
arParms(0) = New SqlParameter("@productID",
SqlDbType.UniqueIdentifier)
arParms(0).Value = productID

'get the images to delete
Dim context As System.Web.HttpContext = System.Web.HttpContext.Current


Dim dr As SqlDataReader = getProductByID(productID)

Dim thumbImg As String
Dim fullImg As String

While dr.Read()
'delete thumb
fullImg = context.Application("webRoot") &
"vendorImages/" & dr.Item("galleryFolder").ToString & "/" &
dr.Item("imageFull").ToString
fullImg = context.Current.Server.MapPath(fullImg)
Dim thisImgFull As New System.IO.FileInfo(fullImg)
thisImgFull.Delete()
End While


.... omitted for brevity


End Su

User submitted from AEWNET (http://www.aewnet.com/)
 
G

Guest

I know, it's very frustrating. I'll keep poking around but I am just
completely stumped. THanks for trying!
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top