ExecuteReader()未将对象引用设置为对象错误的实例

时间:2013-12-20 19:36:47

标签: vb.net visual-studio-2010 visual-studio nullreferenceexception

我试图找出为什么我收到错误的“对象引用未设置为对象的实例”。当我的winforms代码运行时。我在sql语句中设置了一个断点并进入代码并显示为行:使用dr = oledbCmd.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()

DBConnect模块

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

3 个答案:

答案 0 :(得分:3)

我看到了几个错误。查看评论以了解更改的原因

Dim sql As String = "SELECT * from Boxes WHERE Customer = ? AND Status = 'I'"
'.Net uses connection pooling, such that you're better using a new connection object for each query
'Also, a Using block will ensure the connection is closed, **even if an exception is thrown**
' The original code would leak connections when exceptions occured, eventually locking you out of the db
Using cn As New OleDb.OleDbConnection("Connection string here"), _
      cmd As New OleDb.OleDbCommand(sql, cn) 'set CommandText BEFORE adding parameters

    'Use explicit parameter types
    cmd.Parameters.Add("?", SqlDbType.NVarChar, 50).Value = cmbCustomer.Text

    cn.Open()
    Using dr As OleDb.OleDbDataReader = cmd.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
        dr.Close()

    End Using
End Using

答案 1 :(得分:1)

您未能将SqlConnection正确绑定到SqlCommand对象。

Using connection As New SqlConnection(connectionString)
    connection.Open()

    Dim command As New SqlCommand(queryString, connection)
    Dim reader As SqlDataReader = command.ExecuteReader()
    While reader.Read()
        Console.WriteLine("{0}", reader(0))
    End While 
End Using 

请参阅:MSDN

编辑:请求调整以帮助澄清:

    Using connection As New Data.SqlClient.SqlConnection
        Dim sql As String = "SELECT * from Boxes WHERE Customer = ? AND Status = 'I'"
        connection.Open()
        Using command As New Data.SqlClient.SqlCommand(Sql, connection)
            command.Parameters.AddWithValue("@p1", cmbCustomer.Text)
            dr = command.ExecuteReader()

            While dr.Read()
                Dim LV As New ListViewItem
                With LV
                    .UseItemStyleForSubItems = False
                    MediaTypeNames.Text = dr(1).ToString()
                    .SubItems.Add(dr(2).ToString())

                End With
                lvSelectRequestItems.Items.Add(LV)
            End While
        End Using
    End Using

您的代码应该像那样

答案 2 :(得分:1)

这里有相当多的潜在问题。

1 - 您正在使用两次dr变量。一次使用之前,这可能会导致您的错误。然后再次使用,这看起来不正确,因为它不是用于执行阅读器的cmd变量。所以改变你的代码的这一部分:

cmd.CommandText = sql
cmd.Connection = oledbCnn
dr = cmd.ExecuteReader

Using dr = oledbCmd.ExecuteReader()

到此:

cmd.CommandText = sql
cmd.Connection = oledbCnn

Using dr = cmd.ExecuteReader()

2 - 你没有显示oledbCnn是否已被打开。我假设您的DBConnection.connect()函数正在执行此操作,olebCnn是一个函数设置和打开的变量

3 - 我不确定查询字符串中的问号是否有效。即使它确实应该用参数名称替换它。所以你的查询字符串应该是:

sql = "SELECT * from Boxes WHERE Customer = @p1 AND Status = 'I'"

最后你应该显示sub(或函数)的所有代码,这样我们就可以得到一个完整的图片。示例dr必须在使用之前声明,否则会出现构建错误。