I know I am doing something really dim here because it's a really simple bit of code, but I just cannot see it.
I have a class which has a single property of type Dictionary. This class simply looks up against a database table snd stores some values in the dictionary property. Trouble is, I am getting a System.NullReferenceException on the LookupValues.Add line in the code below:
Public Class Lookup
Private _LookupValues As Dictionary(Of Integer, String)
Public Sub New(LookupToRetrieve As String)
Dim sqlQuery As String
sqlQuery = "SELECT * From LookupValues (NOLOCK) WHERE lookup_name = @LookupName"
Using connection As New SqlConnection(My.Settings.PDFProcessorConnectionString)
connection.Open()
Using comm As New SqlCommand(sqlQuery, connection)
comm.Parameters.Add("@LookupName", SqlDbType.VarChar, 20).Value = LookupToRetrieve
Dim rs As SqlDataReader = comm.ExecuteReader
Dim dt As New DataTable
dt.Load(rs)
For Each row As DataRow In dt.Rows
LookupValues.Add(row.Item("lookup_value"), row.Item("lookup_text"))
Next
End Using
End Using
End Sub
Public Property LookupValues As Dictionary(Of Integer, String)
Get
Return _LookupValues
End Get
Private Set(value As Dictionary(Of Integer, String))
_LookupValues = value
End Set
End Property
End Class
I just cannot see what is wrong here - the query returns valid data, but it will not add it to the dictionary... What am I doing wrong?