views:

25

answers:

1

here is the code

 Function getData(ByVal id As String)
    Dim reader As SqlClient.SqlDataReader
    Dim statement As String
    Dim count As Integer
    Dim temp As Integer

    statement = "SELECT * FROM General WHERE accountid='" + id + "'"
    Conn.Open()
    Comm = New SqlClient.SqlCommand(statement, Conn)
    reader = Comm.ExecuteReader()
    count = reader.FieldCount


    Dim Data(count) As String
    If reader.HasRows Then

        For temp = 0 To count
            Data(temp) = (reader.Item(temp))
        Next temp
    Else
        Console.WriteLine("No rows found.")
    End If

    reader.Close()
    Conn.Close()

    Return Data
End Function

when i run the code the hasRow feild is true but reader.Item(temp) gives error "Invalid attempt to read when no data is present."

Can someone pls help

+3  A: 

You need a While reader.Read() loop. The reader starts at row index -1; the Read() will advance to the first row (well, fetch it) and then you can process it.

That's why your HasRows returns true but you can't retrieve the fields -- you're not positioned at the first row yet.

You may want to do something like this:

If reader.HasRows Then
  While reader.Read()
    For temp = 0 To count
        Data(temp) = (reader.Item(temp))
    Next temp
  End While
Else
    Console.WriteLine("No rows found.")
End If
David Hoerster