SqlDataReader Error

  • Thread starter Thread starter Cameron Frasnelly
  • Start date Start date
C

Cameron Frasnelly

I emulated the code from the .Net Framework help (Titled "Using Stored
Procedures with a Command") and I still receive and error...

Error Received = "Invalid attempt to read when no data is present."

BUT when I manually run the Stored Procedure and manually type in '3333' it
works just fine by returning the single row for that particular work
order????????

*****More Clues****
- Returning the column name via .GetName works fine
- I am attempting to do this within the class (I don't see why this would be
an issue)
- I have been working on this one single problem for MORE THAN THREE FRI@#%$
HOURS! :) Just wanted to vent...

*****CODE BELOW*******
'Setup the connection

Dim connObj As New SqlConnection(OpsDB_Module.ConnectionString())

'Setup the command to execute

Dim cmdObj As SqlCommand = New SqlCommand("spWorkOrderFull_byKEY", connObj)

cmdObj.CommandType = CommandType.StoredProcedure

'----add parameter to stored procedure

Dim myParm As SqlParameter = cmdObj.Parameters.Add("@WorkOrderKEY",
SqlDbType.Int)

myParm.Value = 3333

'Open the connection

connObj.Open()

'Grab the data into a reader object

Dim sqlDR As SqlDataReader = cmdObj.ExecuteReader()

'Assign returned values to class properties

Dim tmpString As String = sqlDR.GetName(0) '!!! This Works Just Fine and
returns the column name!!!

Me._WorkOrderKEY = sqlDR.GetInt16(0)

Me._Problem = sqlDR.GetString(sqlDR.GetOrdinal("Problem"))

Me._Priority = sqlDR.GetInt16(sqlDR.GetOrdinal("Priority"))
 
Hello Cameron,

after you get the reader, you need to call the Read method on it so that it
moves to the first or next valid row. When there are no more rows
available, Read will then return false. Here's an example:

Do While myReader.Read()
myReader.GetInt16(0)
' ...
Loop

HTH

Antoine
Microsoft Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
Hello,

Cameron Frasnelly said:
I emulated the code from the .Net Framework help (Titled "Using Stored
Procedures with a Command") and I still receive and error...

Notice that there is a newsgroup for .NET+database (ADO .NET) questions:

news://msnews.microsoft.com/microsoft.public.dotnet.framework.adonet

HTH,
Herfried K. Wagner
 
Back
Top