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 HasRows field is true but reader.Item(temp) gives the error
Invalid attempt to read when no data is present.
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: