开发者

Error - Object reference not set to an instance of an object

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.FindCon开发者_JAVA百科trol("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?


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")


Your FindControl call is returning 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜