Why does this code not work???

  • Thread starter Thread starter Kelly E
  • Start date Start date
K

Kelly E

Here's my function. I keep getting 'Object not set to an
instance of an object when I call myDR.Read. (I'm using
the Application Data blocks v1). I have similar routines
throughout this project that work - I don't see any code
differences. Any suggestions???

Private Function GetOrderSessionId(ByVal OrderId As
Integer) As String

Dim myDR As SqlDataReader
myDR = SqlHelper.ExecuteReader
(ConfigurationSettings.AppSettings("connectionString"), _
CommandType.Text, "Select SessionId from
Orders where OrderId = " & OrderId)

If myDR.Read = True Then 'ERROR HERE
Return myDR("SessionId")
End If
myDR.Close()

End Function
 
SqlHelper is the MS Application Block object. The
application blocks are classes MS built to make our lives
easier in writing data access code. I use the object
everywhere in this project.
 
Any suggestions???

Try running the SQL query in Query Analyzer. It's possible that the query
does not return anything, so the myDR datareader is null.
Dim myDR As SqlDataReader
myDR = SqlHelper.ExecuteReader
(ConfigurationSettings.AppSettings("connectionString"), _
CommandType.Text, "Select SessionId from
Orders where OrderId = " & OrderId)

If you put a break point at the line above and then step through it, is the
myDR variable Nothing? Again, it sounds like your SQL query is not return
anything. Is the OrderId valid and are there items in the Orders table for
that OrderId?

Just some thoughts
 
Kelly,

What namespace does the SQLHelper reside, I could not find it in the help?
I use a similar pattern for returning a row or rows of data and do not have
a problem (shown below). As I suggested before the error must reside in
some where in/around SQLHelper because the reader is not properly
instantiated, (perhaps the connect string is incorrect).

Even if your query does not return rows the Reader.Read method should return
a value.

Dan

If rdr.Read() Then
...
End If

While rdr.Read
...
End While
 
Back
Top