我正在尝试使用 DataTable 填充列表,我有一个 for each 循环来检查每一行并将项目添加到列表中。但是代码不起作用,我不断收到错误..
System.NullReferenceException:{“对象引用未设置为对象的实例。”}
-数据:{System.Collections.ListDictionaryInternal}
-HelpLink:无 -内部异常:无
-TargetSite:{System.Collections.Generic.List`1[System.String] getListOfUsers()}
这是我的代码...
Function getListOfUsers() As List(Of String)
'Dim i As Integer = 0
Dim lUserNames As List(Of String) = Nothing
Dim dt As DataTable = getDataTable(db_Config, "SELECT * FROM tblUsers")
If dt.Rows.Count > 0 Then
Try
For Each dRowItem As DataRow In dt.Rows
'i = i + 1
'If IsDBNull(dt.Rows(0)("fldUserName").ToString) = False Then
' lUserNames.Add(dt.Rows(0)("fldUserName").ToString)
'End If
If dRowItem.Item("fldUserName").ToString <> "" Then
lUserNames.Add(dRowItem.Item("fldUserName").ToString)
End If
Next dRowItem
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
Return lUserNames
End Function