Home » Category » Web Forms

Web Forms: Yet another Null Reference error...any ideas anybody?

102| Sun, 06 Jan 2008 06:28:00 GMT| mdcragg| Comments (3)

I am getting a Null Reference error. I am trying to harvest data from a CreateUserWizard using the lines of code below. I am then trying to pass that data to a procedure which posts the information in a database.

When I try this with only the CWZ.UserName value, it works. When I add the other custom controls that I added to the CreateUserWizard it fails with the error shown further below.

The CreateUserWizard is nested in a LoginView which is in turn nested in a ContentPlaceHolder.

Any ideas anybody? Thanks in advance.

<<<<<<<<<<<<<<<<<<<<<my code>>>>>>>>>>>>>>>>>>>>>>>>>>>


Sub CreateUserWizard_CreatedUser(ByVal sender As Object, ByVal e As EventArgs)

Dim CWZ As CreateUserWizard
CWZ = CType(Me.LoginView1.FindControl("CreateUserWizard"), Wizard)

Dim FirstName As TextBox
FirstName = CType(CWZ.FindControl("txtFirstName"), TextBox)

Dim LastName As TextBox
LastName = CType(CWZ.FindControl("txtLastName"), TextBox)

Dim Country As TextBox
Country = CType(CWZ.FindControl("ddlCountry"), TextBox)

Dim PostalCode As TextBox
PostalCode = CType(CWZ.FindControl("txtPostalCode"), TextBox)

CreateUserProfile(CWZ.UserName, FirstName.Text, LastName.Text, Country.Text, PostalCode.Text)

End Sub


Private Sub CreateUserProfile(ByVal UserName As String, ByVal FirstName As String, ByVal LastName As String, ByVal Country As String, ByVal PostalCode As String)
'Private Sub CreateUserProfile(ByVal userName As String)
Dim conString As String = WebConfigurationManager.ConnectionStrings("UserProfiles").ConnectionString
Dim con As New SqlConnection(conString)
Dim cmd As New SqlCommand("INSERT profiles_BasicProperties (UserName,FirstName,LastName,Country,PostalCode) VALUES (...UserName,...FirstName,...LastName,...Country,...PostalCode)", con)
cmd.Parameters.AddWithValue("...UserName", UserName)
cmd.Parameters.AddWithValue("...FirstName", FirstName)
cmd.Parameters.AddWithValue("...LastName", LastName)
cmd.Parameters.AddWithValue("...Country", Country)
cmd.Parameters.AddWithValue("...PostalCode", PostalCode)
Using con
con.Open()
cmd.ExecuteNonQuery()
End Using
End Sub

<<<<<<<<<<<<<<<<<<<<<<<<<<<the error that I get (Line 29 is highlighted as the source of the error)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 27: PostalCode = CType(CWZ.FindControl("txtPostalCode"), TextBox)
Line 28:
Line 29: CreateUserProfile(CWZ.UserName, FirstName.Text, LastName.Text, Country.Text, PostalCode.Text)
Line 30: 'CreateUserProfile(CWZ.UserName)
Line 31: 'CreateUserWizard_SendingMail(CWZ.UserName, e)

Source File: C:\Users\mdcragg\Documents\site\PC_Dev\User_Create.aspx Line: 29

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
ASP.user_create_aspx.CreateUserWizard_CreatedUser(Object sender, EventArgs e) in C:\Users\mdcragg\Documents\site\PC_Dev\User_Create.aspx:29
System.Web.UI.WebControls.CreateUserWizard.OnCreatedUser(EventArgs e) +105
System.Web.UI.WebControls.CreateUserWizard.AttemptCreateUser() +341
System.Web.UI.WebControls.CreateUserWizard.OnNextButtonClick(WizardNavigationEventArgs e) +105
System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e) +453
System.Web.UI.WebControls.CreateUserWizard.OnBubbleEvent(Object source, EventArgs e) +149
System.Web.UI.WebControls.WizardChildTable.OnBubbleEvent(Object source, EventArgs args) +17
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +163
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.312; ASP.NET Version:2.0.50727.312

Keywords & Tags: null, reference, error, ideas, anybody, web, forms

URL: http://dotnet.itags.org/web-forms/105/
 
«« Prev - Next »» 3 helpful answers below.

Can you show your CreateUserProfile code?

thejd | Sun, 06 Jan 2008 06:29:00 GMT |

It's there...in my original post. I don't think the CreateUserProfile piece is the problem though. I put text boxes on the form outside the loginview and the create user wizard (to eliminate nesting issues). The text boxes corresponded to the values passed to the CreateUserProfile piece. I tried running the form with these non-nested controls and everything worked. So it must be something wrong with the FindControl lines in the CreateUserWizard_CreatedUser sub.

mdcragg | Sun, 06 Jan 2008 06:30:00 GMT |

Okay...I fixed my own problem. I have to reference the controls in the wizard like this:

Dim FirstNameAs TextBox

FirstName =CType(CWZ.CreateUserStep.ContentTemplateContainer.FindControl("txtFirstName"), TextBox)

mdcragg | Sun, 06 Jan 2008 06:31:00 GMT |

Web Forms Hot Answers

Web Forms New questions

Web Forms Related Categories