For i = 0 to DTName.Rows.count
IndexOutOfRangeException
当值i
等于时最终会抛出错误DTName.Rows.count
,限制应该是DTName.Rows.count - 1
。
要从所有数据行中获取所有值,请将它们存储在List
:
Dim strName As New List(Of String)
For i = 0 to DTName.Rows.count - 1
strName.Add(DTName.Rows(i)("Client Name").Tostring())
Next i
或者,您可以Foreach
像这样使用:
For Each DR As DataRow In DTName.Rows
strName.Add(DR("Client Name").Tostring())
Next
我还建议您删除多余的检查,如果DTName.Rows.Count > 0
编辑:您可以声明strName
为string
并将行值附加到它:
For i = 0 to DTName.Rows.count - 1
strName &= (DTName.Rows(i)("Client Name").Tostring() & ",")
Next i
Response.Write(strName)