错误 - 未将对象引用设置为对象的实例
当我运行以下代码时,出现以下错误:
“未将对象引用设置为对象的实例。”
Protected Sub CreateUserWizard1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.Load
Dim SQLData As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True")
Dim cmdSelect As New System.Data.SqlClient.SqlCommand("SELECT TOP 1 EmployeeId FROM a1_admins Order by Id DESC", SQLData)
Dim label11 As Label = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Label11")
SQLData.Open()
Dim dtrReader As System.Data.SqlClient.SqlDataReader = cmdSelect.ExecuteReader()
If dtrReader.HasRows Then
While dtrReader.Read()
label11.Text = dtrReader("EmployeeId")
End While
End If
dtrReader.Close()
SQLData.Close()
End Sub
End Class
我该如何解决这个问题?
When I run the following code it thows the following error:
"Object reference not set to an instance of an object."
Protected Sub CreateUserWizard1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.Load
Dim SQLData As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True")
Dim cmdSelect As New System.Data.SqlClient.SqlCommand("SELECT TOP 1 EmployeeId FROM a1_admins Order by Id DESC", SQLData)
Dim label11 As Label = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Label11")
SQLData.Open()
Dim dtrReader As System.Data.SqlClient.SqlDataReader = cmdSelect.ExecuteReader()
If dtrReader.HasRows Then
While dtrReader.Read()
label11.Text = dtrReader("EmployeeId")
End While
End If
dtrReader.Close()
SQLData.Close()
End Sub
End Class
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有堆栈跟踪很难说(也许你可以提供它?),但我的猜测是,查看你的代码,以下行返回 null
并且你试图在这里设置它的“Text”属性,即使变量为 null(访问 null 对象上的属性)
It's tough to say without the stack trace (maybe you can provide it?), but my guess, looking at your code is that the following line is returning null
and you are trying to set it's "Text" property here, even though the variable is null (accessing a property on a null object)
您的
FindControl
调用返回null
。Your
FindControl
call is returningnull
.只需尝试将
label11.Text = dtrReader("EmployeeId")
替换为以下内容,然后检查异常是否仍然出现。如果未出现异常,则意味着您的
FindControl
方法无法找到 ID 为“Label11
”的控件,因此为label11 分配了 null 值
。Just try replacing
label11.Text = dtrReader("EmployeeId")
with the following and check if the exception still arises.If the exception does not show up, that means your
FindControl
method is not able to find the control with ID 'Label11
' and thus assigning a null value tolabel11
.