错误 - 未将对象引用设置为对象的实例

发布于 2024-10-05 01:58:07 字数 964 浏览 2 评论 0原文

当我运行以下代码时,出现以下错误:

“未将对象引用设置为对象的实例。”

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

靑春怀旧 2024-10-12 01:58:07

没有堆栈跟踪很难说(也许你可以提供它?),但我的猜测是,查看你的代码,以下行返回 null

Dim label11 As Label = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Label11")

并且你试图在这里设置它的“Text”属性,即使变量为 null(访问 null 对象上的属性)

label11.Text = dtrReader("EmployeeId")

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

Dim label11 As Label = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Label11")

and you are trying to set it's "Text" property here, even though the variable is null (accessing a property on a null object)

label11.Text = dtrReader("EmployeeId")
青衫负雪 2024-10-12 01:58:07

您的 FindControl 调用返回 null

Your FindControl call is returning null.

世态炎凉 2024-10-12 01:58:07

只需尝试将 label11.Text = dtrReader("EmployeeId") 替换为以下内容,然后检查异常是否仍然出现。

If Not label11 Is Nothing Then
label11.Text = dtrReader("EmployeeId") 
End If

如果未出现异常,则意味着您的 FindControl 方法无法找到 ID 为“Label11”的控件,因此为 label11 分配了 null 值

Just try replacing label11.Text = dtrReader("EmployeeId") with the following and check if the exception still arises.

If Not label11 Is Nothing Then
label11.Text = dtrReader("EmployeeId") 
End If

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 to label11.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文