Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
DataReader Issue
Message
De
13/03/2003 21:29:12
 
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Divers
Thread ID:
00765602
Message ID:
00765674
Vues:
17
This message has been marked as the solution to the initial question of the thread.
I try to simulate your error. Because I have no my own SP, which returns only one record, I tried this with sp_help standard SP (returns one row if you specify correct objname) and pass name of the one table into Northwind database, which comes with SQL Server. So I try this code (and I want you also to test is and tell me the results):
        Dim sqlCon As SqlConnection = New SqlConnection("Password=my_password;Persist Security Info=True;User ID=plamen;Initial Catalog=Northwind;Data Source=OUR_SERVER")
        sqlCon.Open()
        Dim sqlCmd As SqlCommand = New SqlCommand()
        sqlCmd.CommandType = CommandType.StoredProcedure
        sqlCmd.CommandText = "sp_help"
        Dim param As SqlParameter = sqlCmd.Parameters.Add("@objname", SqlDbType.NVarChar, 776)
        param.Direction = ParameterDirection.Input
        param.Value = "Categories"
        sqlCmd.Connection = sqlCon

        Dim rs As SqlDataReader = sqlCmd.ExecuteReader()
        Dim s As Integer = 0
        Do While rs.Read()
            s = s + 1
        Loop
I use "s" variable only to have statement inside the loop. Set breakpoint to the "Do While" row and start this code.

sp_help returns reader with four fields, so in watch window I insert these four statements:
rs(0)
rs(1)
rs(2)
rs(3)
When code stops at the breakpoint, all statements shows "Run-time exception: InvalidOperationException...", so the first (and the only) row was not readed. When I step over "Do While" row, the statements into debugger shows correct values. When after "Loop" I go to the "Do While" row for the second time, Read() method returns False and I leave the loop.

Everything works as documentation says. The only thing which comes in head is to make sure your procedure returns the row. Also, if you have other code, which is not posted in your message, make sure you do not use Read() method in it.

Plamen Ivanov
MCSD .NET Early Achiever and MCAD .NET Charter Member (VB .NET/SQL Server 2000)
MCSD (VB 6.0/SQL Server 2000)

VB (.NET) - what other language do you need in the whole Universe?...

Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform