【发布时间】:2018-09-28 10:31:37
【问题描述】:
我知道我在这里做了一些非常模糊的事情,因为它是一段非常简单的代码,但我就是看不到它。
我有一个类,它有一个字典类型的单一属性。此类只是查找数据库表,并将一些值存储在字典属性中。麻烦的是,我在下面的代码中的 LookupValues.Add 行上得到了 System.NullReferenceException:
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
我只是看不到这里有什么问题 - 查询返回有效数据,但它不会将其添加到字典中......我做错了什么?
【问题讨论】:
-
_LookupValues = New Dictionary(Of Integer, String) 丢失。
标签: vb.net