DTName is a DataTable which returns rows in the code below…when I iterate it through the loop from 0 to its count. Only the data in first row is displayed.
How to get all the rows of the datatable displayed in the result ?
DTName = GetClientNames()
If Not DTName Is Nothing Then
For i = 0 to DTName.Rows.count
strName = DTName.Rows(i).Item("Client Name").Tostring()
Next i
End if
For i = 0 to DTName.Rows.countwould eventually throw aIndexOutOfRangeExceptionerror when the value ofiequals toDTName.Rows.count, the limit should beDTName.Rows.count - 1.To get all the values from all datarows, store them in a
List:Alternatively you could use
Foreachlike this :I also suggest you remove the redundant check if
DTName.Rows.Count > 0EDIT : You could Declare
strNameasstringand append row values to it :