我想弄清楚爲什麼我得到錯誤'對象引用未設置爲對象的實例。'當我的winforms代碼運行。我設置了斷點的SQL語句,並步入了代碼,並顯示爲行:使用DR = oledbCmd.ExecuteReader()ExecuteReader()對象引用未設置爲對象的實例錯誤
我仍然在學習vb.Net所以希望得到一些幫助,如何克服這個錯誤。非常感謝
DBConnection.connect()
sql = "SELECT * from Boxes WHERE Customer = ? AND Status = 'I'"
Dim cmd As New OleDb.OleDbCommand
cmd.Parameters.AddWithValue("@p1", cmbCustomer.Text)
cmd.CommandText = sql
cmd.Connection = oledbCnn
dr = cmd.ExecuteReader
Using dr = oledbCmd.ExecuteReader()
While dr.Read()
Dim LV As New ListViewItem
With LV
.UseItemStyleForSubItems = False
.Text = dr(1).ToString()
.SubItems.Add(dr(2).ToString())
End With
lvSelectRequestItems.Items.Add(LV)
End While
End Using
cmd.Dispose()
dr.Close()
oledbCnn.Close()
數據庫連接模塊
Imports System.Data.OleDb
Module DBConnection
Public connetionString As String = My.Settings.storageConnectionString
Public oledbCnn As New OleDbConnection
Public oledbCmd As OleDbCommand
Public dr As OleDbDataReader
Public sql As String
Sub connect()
'connetionString = My.Settings.storageConnectionString
oledbCnn.ConnectionString = connetionString
oledbCnn.Open()
End Sub
End Module
幾乎所有'NullReferenceException'的情況都是相同的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 –