createuserwizard nullrefrence exception
CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl(
"FirstName") function is returning a null and I get an object refrence not set to an instance of an object error.Please help, thanks in advance.
createaccount.aspx.vb follows
----
Imports System.Data.SqlClient
Partial Class CreateAccount Inherits System.Web.UI.PageProtected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser Dim UserNameTextBox As TextBoxDim User As MembershipUser Dim UserGUID As ObjectUserNameTextBox = CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName")User = Membership.GetUser(UserNameTextBox.Text)
UserGUID = User.ProviderUserKey
Using conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) Dim cmd As SqlCommand = conn.CreateCommand()Dim FirstNameTextBox, LastNameTextBox, CityTextBox, PostcodeTextBox As TextBox Dim SexDDL, DOBDayDDL, DOBMonthDDL, DOBYearDDL, RegionDDL, StateDDL, CountryDDL As DropDownListFirstNameTextBox = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")LastNameTextBox = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl(
"LastName")SexDDL = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl(
"SexDropDownList")PostcodeTextBox = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Postcode")cmd.CommandText =
"INSERT INTO UserInfo(UserID, Administrator, FirstName, LastName, SexMale, DOB, City, RegionID, StateID, CountryID, PostCode) VALUES (@UserID, 0, @FirstName, @LastName, @Sex, @DOB, @City, @Region, @State, @Country, @PostCode)"cmd.Parameters.AddWithValue("@UserID", UserGUID.ToString())cmd.Parameters.AddWithValue(
"@FirstName", FirstNameTextBox.Text)cmd.Parameters.AddWithValue("@LastName", LastNameTextBox.Text)cmd.Parameters.AddWithValue(
"@Sex", If(SexDDL.SelectedValue = "Male", 1, 0))cmd.Parameters.AddWithValue("@PostCode", PostcodeTextBox.Text)cmd.ExecuteNonQuery()
End UsingEnd SubEnd
Class----
createaccount.aspx follows
----
<%@ Page Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="CreateAccount.aspx.vb" Inherits="CreateAccount" title="Untitled Page" EnableSessionState="False" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server"><title>Untitled Page</title></
head><
body><form id="form1" runat="server"><div><p class="style1">Create a New Account
</p><p class="style2">In order to fully use this site, you'll need to first create a new user account.Fill out the following form to create a new account.
</p><p class="style2"><asp:CreateUserWizard ID="CreateUserWizard1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="1px" CancelDestinationPageUrl="~/Default.aspx" ContinueDestinationPageUrl="~/Default.aspx" DisplayCancelButton="True" Font-Names="Verdana" Font-Size="0.8em" style="font-size: medium" EnableViewState="False"><SideBarStyle BackColor="#5D7B9D" BorderWidth="0px" Font-Size="0.9em" VerticalAlign="Top" /><SideBarButtonStyle BorderWidth="0px" Font-Names="Verdana" ForeColor="White" /><ContinueButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" /><NavigationButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" /><HeaderStyle BackColor="#5D7B9D" BorderStyle="Solid" Font-Bold="True" Font-Size="0.9em" ForeColor="White" HorizontalAlign="Center" /><CreateUserButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" /><TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /><StepStyle BorderWidth="0px" /><WizardSteps><asp:WizardStep ID="WizardStep1" runat="server" Title="Your Details"><table border="0" style="font-family:Verdana;font-size:100%;"><tr><td align="center" colspan="2" style="color:White;background-color:#5D7B9D;font-weight:bold;">
Sign Up for Your New Account
</td></tr><tr><td align="right"><asp:Label ID="FirstNameLabel" runat="server" AssociatedControlID="FirstName">FirstName:
</asp:Label></td><td><asp:TextBox ID="FirstName" runat="server" Width="270px"></asp:TextBox><asp:RequiredFieldValidator ID="FirstNameRequired" runat="server" ControlToValidate="FirstName" ErrorMessage="First Name is required." ToolTip="First Name is required.">*</asp:RequiredFieldValidator></td></tr><tr><td align="right"><asp:Label ID="LastNameLabel" runat="server" AssociatedControlID="LastName">LastName:
</asp:Label></td><td><asp:TextBox ID="LastName" runat="server" Width="270px"></asp:TextBox><asp:RequiredFieldValidator ID="LastNameRequired" runat="server" ControlToValidate="LastName" ErrorMessage="Last Name is required." ToolTip="Last Name is required.">*</asp:RequiredFieldValidator></td></tr><tr><td align="right"><asp:Label ID="SexLabel" runat="server" AssociatedControlID="SexDropDownList">Sex:</asp:Label></td><td><asp:DropDownList ID="SexDropDownList" runat="server"><asp:ListItem>Select One</asp:ListItem><asp:ListItem>Male</asp:ListItem><asp:ListItem>Female</asp:ListItem></asp:DropDownList><asp:CompareValidator ID="SexCompareValidator" runat="server" ControlToValidate="SexDropDownList" ErrorMessage="Sex is required." Operator="NotEqual" ValueToCompare="Select One">*</asp:CompareValidator></td></tr><td align="right">
<asp:Label ID="PostcodeLabel" runat="server" AssociatedControlID="Postcode">Post/ZipCode:</asp:Label></td><td><asp:TextBox ID="Postcode" runat="server" Width="80px"></asp:TextBox><asp:RequiredFieldValidator ID="PostcodeRequired" runat="server" ControlToValidate="Postcode" ErrorMessage="Post/Zipcode is required." ToolTip="Postcode is required.">*</asp:RequiredFieldValidator></td></tr></table></asp:WizardStep><asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" ><ContentTemplate><table border="0" style="font-family:Verdana;font-size:100%;"><tr><td align="center" colspan="2" style="color:White;background-color:#5D7B9D;font-weight:bold;">Sign Up for Your New Account
</td></tr><tr><td align="right"><asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">UserName:
</asp:Label></td><td><asp:TextBox ID="UserName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator></td></tr><tr><td align="right"><asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td><td><asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator></td></tr><tr><td align="right"><asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label></td><td><asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword" ErrorMessage="Confirm Password is required." ToolTip="Confirm Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator></td></tr><tr><td align="right"><asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label></td><td><asp:TextBox ID="Email" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator></td></tr><tr><td align="right"><asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">SecurityQuestion:
</asp:Label></td><td><asp:TextBox ID="Question" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="QuestionRequired" runat="server" ControlToValidate="Question" ErrorMessage="Security question is required." ToolTip="Security question is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator></td></tr><tr><td align="right"><asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">SecurityAnswer:
</asp:Label></td><td><asp:TextBox ID="Answer" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer" ErrorMessage="Security answer is required." ToolTip="Security answer is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator></td></tr><tr><td align="right"><asp:Label ID="ValidateLabel" runat="server" AssociatedControlID="Answer">Validate:</asp:Label></td></tr><tr><td align="center" colspan="2"><asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match." ValidationGroup="CreateUserWizard1"></asp:CompareValidator></td></tr><tr><td align="center" colspan="2" style="color:Red;"><asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal></td></tr><tr><td align="center" colspan="2"><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid E-mail address." ControlToValidate="Email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"></asp:RegularExpressionValidator></td></tr></table></ContentTemplate></asp:CreateUserWizardStep><asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" /></WizardSteps></asp:CreateUserWizard></p></div>
</form></
body></
html>
Hi
Here is an example about how to dynamicly get FirstName of CreateUserWizard control, Hope it helps
Dim FirstNameTextBox As TextBox = WizardStep1.FindControl("FirstName")
Response.Write(FirstNameTextBox.Text)
Thanks XiaoYongDai, I have already tried that and although it returns a textbox control for 'FirstNameTextbox' the 'FirstNameTextbox.text' is = "".
I should mention that I am using the following for my web.config in case it effects anything.
---
<membership><
providers><clear/><
add name="AspNetSqlMembershipProvider"connectionStringName="ConnectionString"applicationName="/"type="System.Web.Security.SqlMembershipProvider" /></
providers></membership><
roleManager enabled="true"><providers><
clear/><
add name="AspNetSqlRoleProvider"connectionStringName="ConnectionString"applicationName="/"type="System.Web.Security.SqlRoleProvider"/></
providers></
roleManager>Hi
Step value cannot pass to the next step, for exampe, you can test the same code in CreateUserWizard1_ActiveStepChanged event, only the first step will return "FirstName".
My work around is store those information temporary in session, or change the order of wizard steps. Hope it helps
- Cannot access username from LoginView > AnonymousTemplate > Login ... control.
- Upload file to another server + authentication
- "remember me" function in Login control
- ixwebhosting and web.config
- <authentication mode="Windows" />
- problem with login control
- System.UnauthorizedAccessException though Network Service acct has Full Control Permissions
- Forms Authentication Question
- set connectionstring programmaticallyfor custom membership provider
- adding roles in application.
Rss Feed