简体   繁体   English

未使用对象实例设置的对象变量

[英]Object variable not set with an instance of an object

Just because (in my code) some times looks the sqlDataReader to be open and tells me that "there is already an open data reader" 只是因为(在我的代码中)有时看起来sqlDataReader是打开的,并告诉我“已经有一个开放的数据阅读器”
I decide to put this line: If Not SqlReader.IsClosed Then SqlReader.Close() 我决定把这行: If Not SqlReader.IsClosed Then SqlReader.Close()

Select Case PreviousRecord
    Case True
        SqlComm = New SqlCommand("Select * from " & tmpName & " where FuelOrderValid = '" & True & "' Order by FuelLoadDate", ReportsSQLConn)
    Case False
        SqlComm = New SqlCommand("Select * from " & tmpName & " where FuelOrderValid = '" & True & "' And FuelOrderID = '" & ordNum & "' Order by FuelLoadDate", ReportsSQLConn)
End Select      

If Not SqlReader.IsClosed Then SqlReader.Close()

If SqlComm.Connection.State = Data.ConnectionState.Open Then
    SqlReader = SqlComm.ExecuteReader(CommandBehavior.KeyInfo)
Else
    SqlComm.Connection.Open()
    SqlReader = SqlComm.ExecuteReader(CommandBehavior.KeyInfo)
End If

But now, when it comes to execute the instruction 但现在,当涉及到执行指令时
If Not SqlReader.IsClosed Then SqlReader.Close()
It gives the error: 它给出了错误:
Object Variable Not Set To An Instance Of An Object
I really can't understand why is doing this. 我真的不明白为什么这样做。
Please is there anybody to assist me? 请问有人帮我吗?

The SqlReader has not been initialised before calling SqlReader.IsClosed and SqlReader.Close . SqlReader还没有被调用之前初始化SqlReader.IsClosedSqlReader.Close You can check whether its nothing like this: 您可以检查它是否nothing这样的:

If SqlReader IsNot Nothing Then
    ' Do something with the SqlReader'
Else
    ' Create a new SqlReader'
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM