Question Retrieve specific field value from ArrayList

mujaqo

New member
Joined
Oct 28, 2015
Messages
2
Programming Experience
Beginner
I use the below code to store SqlDataReader data in ArrayList as objects, and I couldn't know how to retrieve the data stored in Row1 Column3 as string, can anyone help please?

Dim myReader As SqlDataReader
myReader = mySqlCommand.ExecuteReader() 'Retrieve Data from SQL Server
If myReader.HasRows Then
Dim myArrayList As New ArrayList()
While myReader.Read()
Dim values(myReader.FieldCount - 1) As Object
Dim fieldCount As Integer = myReader.GetValues(values)
myReader.GetValues(values) 'Get the Row with all its column values
myArrayList.Add(values) 'Add this Row to ArrayList
End While
End if
 
Hi and Welcome to the Forum,

I would recommend a small change of tact here and load your DataReader into a Datatable instead since this then gives you access to simple Row and Column Indexes that you can work with. If you said:-

Dim someDataTable As New DataTable
myReader = sqlCmnd.ExecuteReader
someDataTable.Load(myReader)


You then get a Datatable with rows and columns that you can easily iterate through. To get the third column in the first row you can then say:-

Dim row1Col3Data As String = someDataTable.Rows(0)(2).ToString
MsgBox(row1Col3Data)



And, if you want to iterate through all the rows in the Datatable and show all the values in column 3 then you could say:-

For Each currentRow As DataRow In someDataTable.Rows
  MsgBox(currentRow(2).ToString)
Next


Hope that helps.

Cheers,

Ian
 
Last edited:
Back
Top