I'm trying to populate a List using a DataTable, I have a for each loop that checks every row and adds the item to the list. But the code isn't working, I keep getting the error..
System.NullReferenceException: {"Object reference not set to an instance of an object."}
-Data: {System.Collections.ListDictionaryInternal}
-HelpLink: Nothing -Inner Exception: Nothing
-TargetSite: {System.Collections.Generic.List`1[System.String] getListOfUsers()}
This is my code...
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