vb.net conversion between datareader and string

liunx

Guest
Dim query As String 'Query to be executed
Dim result() As String 'results of query
query = "select * from drinks where drinks.drinkName = '" & drinkName & "'"
result = connectDB(query)

''''''''''''''''''''''''''''''''''''''''''''''''

Private Function connectDB(ByVal query As String)
dbConnect.Open() 'Open database connection
Dim dr As OleDb.OleDbDataReader 'Define datareader
dbQuery = New OleDb.OleDbCommand(query, dbConnect) 'Define data command
dr = dbQuery.ExecuteReader 'Execute command
Dim results(dr.Depth, dr.FieldCount) As String 'create string array
Dim x As Int16 'counter
Dim i As Int16 'counter
If dr.Read() Then 'see if record exists
For x = 0 To dr.Depth
For i = 0 To dr.FieldCount
results(x, i) = dr(x, i).tostring
Next
dr.Read() ' go to next record
Next
End If
dr.Close() 'close db connection
dbConnect.Close()
Return results
End Function



I am running into trouble converting the datareader to a string. the underlined part is giving me an error that says

Form1.vb(784): Overload resolution failed because no accessible 'Item' accepts this number of arguments.

now x is a veriable that counts to dr.depth (number of rows) and i counts to dr.fieldcount.

I feel like im making prgress in figuring this out but just seem to run into problems I cant seem to figure out how to fix. Is the conversion right from data reader to a string.

what am I doing wrong

tiaOff the top of my head I see a few potential issues.
First I've never used Depth but there is no need to know the number of rows if all you're going to do is use them for a loop. Just use a while loop and test if dr.Read is null (Nothing in VB?). Doing this means not only do you not need the Depth property but you can drop the whole x For loop. Just initialize your x variable to 0 when you Dim it and increment it at the end of the while loop and you should be good.

The second potential issue I see is that since the count you are using is zero based you should be looping to FieldCount - 1.

The error you are getting though is because there are not two arguments to the datareader. All you should need in the underlined text is "dr(i)" because there is only one row in the datareader on any single run through the loop.

Finally not sure about VB but you might have to throw the parens after the ToString call.
 
Back
Top