Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataReader Not returning rows

Status
Not open for further replies.

traceytr

Programmer
Mar 13, 2001
94
US
Hello.
I'm pretty new to VB.NET, and I'm trying to get my .NET code to execute a SQL Server stored procedure and return the recordset. The stored procedure is executing, but no rows are returned into the DataReader as indicated by my message box. Can anyone tell me what I'm missing? Thanks.

Tracey
[ponder]

Code:
'Connection - Declare and open connection        
Dim myConnectionString As String = "Integrated Security=XXXX;Persist Security Info=False;Initial Catalog=DataBaseName;Data Source=ServerName;Packet Size=XXXX"
Dim myConnection As SqlConnection = New SqlConnection(myConnectionString)
myConnection.Open()

'Command
Dim mySqlCommand As SqlCommand = New SqlCommand("sp_myStoredProcedure", myConnection)

'DataReader - Declare DataReader and populate with result set from stored procedure.
Dim myReader As SqlDataReader = mySqlCommand.ExecuteReader()

Try
    'If myReader.HasRows Then
    If myReader.Read <> False Then
       Do While myReader.Read()
           MsgBox("Rows Returned.", MsgBoxStyle.Information)
        Loop
    Else
        MsgBox("No rows returned.", MsgBoxStyle.Information)
    End If

Finally
    myReader.Close()
    myConnection.Close()
End Try
 
I'm pretty new to this too, but I think your problem is that the myReader.read() comes after the .HasRows. It will always not have rows before the read starts.
 
Thank you, Vonehle, for responding -- I appreciate it. I didn't get a chance to try your suggestion, but at the very moment I was reading it the light came on, and I realized why no rows were returned. The stored procedure was executing when I ran the code, but not returning any rows... Basically the stored procedure truncates a data table and then loads it with current data. This was working, so for the life of me I couldn't figure out why the rows weren't being returned. Well, as it turns out the stored procedure loaded the table, but I did not write a select statement to select the new rows. Here I thought it was because I was new to .NET, and all the while it was because I didn't ask for the records in the first place. I totally overlooked the obvious.
 
Good for you, because I was wrong anyway. Lucky for me, your code tipped me off on how to fix something I was working on. So, thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top