-1

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?

Justin
  • 80,106
  • 47
  • 208
  • 350
D Infosystems
  • 494
  • 1
  • 12
  • 43
  • What line of code is throwing the exception? Also, this would be easier to read if you formatted it as code. – Chaulky Dec 01 '10 at 16:31
  • possible duplicate of [Object reference not set to an instance of an object](http://stackoverflow.com/questions/548932/object-reference-not-set-to-an-instance-of-an-object) – Justin Dec 16 '11 at 15:02
  • Actually [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) is better – Justin Dec 16 '11 at 15:05

3 Answers3

0

Your FindControl call is returning null.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
0

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")
Benny
  • 3,759
  • 6
  • 42
  • 81
  • Make sure the name of your label exists. I'm not familiar with the CreateUserWizard control you are using, but the main problem is, it's not finding your control. – Benny Dec 01 '10 at 16:55
0

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.

Dienekes
  • 1,500
  • 1
  • 14
  • 24