开发者

Customizing CreateUserWizard. Help storing info in DB. ASP.NET

I have a create user wizard control and have added some more fields for it and want to store that extra info into another table in the same DB. I have tried several tutorials and none seem to work. The one below, I get error, "Object reference not set to an instance of an object"

What I'm is doing wrong. Maybe there is something extra I need to do for the table I set up? links, etc would help. Thanks

The table name is ExtraUserInfo. The other tables are default setup for membership services....aspnet_reqsql.exe

Here is the source

                            <asp:SqlDataSource ID="ExtraUserInfo" runat="server" ConnectionString="LoginSQL"
         InsertCommand="INSERT INTO [ExtraUserInfo] ([CompanyName]) VALUES (@CompanyName)"
         ProviderName="MySqlLoginProvider">
        <InsertParameters>
            <asp:ControlParameter Name="CompanyName" Type="String" ControlID="CompanyName" PropertyName="Text" />
        </InsertParameters>
    </asp:SqlDataSource> 

c#

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{

    Guid userFKey;

    MembershipUser mu = Membership.GetUser(CreateUserWizard1.UserName);
    userFKey = (Guid)mu.ProviderUserKey;

    TextBox UserNameTextBox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("CompanyName");
    SqlDataSource DataSource = (SqlDataSource)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ExtraUserInfo");

    object UserGUID = mu.ProviderUserKey;

    DataSource.InsertParameters.Add("UserId", UserGUID.ToString());
    DataSource.Insert();
}

Full error

Line 48:             object UserGUID = mu.ProviderUserKey;
Line 49: 
Line 50:             DataSource.InsertParameters.Add("UserId", UserGUID.ToString());
Line 51:             DataSource.Insert();
Line 52: 

Whole createuserwizard source

        <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
            <ContentTemplate>
                <table>
                    <tr>
                        <td align="center" colspan="2">
                            Sign Up for Your New Account</td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="CompanyNameLabel" runat="server" AssociatedControlID="CompanyName">Company Name:</asp:Label></td>
                        <td>
                            <asp:TextBox ID="CompanyName" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="CompanyNameValidator" runat="server" ControlToValidate="CompanyName"
                            ErrorMessage="Company Name is required." ToolTip="Company Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="FirstNameLabel" runat="server" AssociatedControlID="FirstName">First Name:</asp:Label></td>
                        <td>
                            <asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FirstName"
                            ErrorMessage="First Name is required." ToolTip="First Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="LastNameLabel" runat="server" AssociatedControlID="LastName">Last Name:</asp:Label></td>
                        <td>
                            <asp:TextBox ID="LastName" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="LastName"
                            ErrorMessage="Last Name is required." ToolTip="Last Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="AddressLabel" runat="server" AssociatedControlID="Address">Address:</asp:Label></td>
                        <td>
                            <asp:TextBox ID="Address" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="Address"
                            ErrorMessage="Address is required." ToolTip="Address is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="PhoneNumberLabel" runat="server" AssociatedControlID="PhoneNumber">Phone Number:</asp:Label></td>
                        <td>
                            <asp:TextBox ID="PhoneNumber" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="PhoneNumber"
                            ErrorMessage="Phone Number is required." ToolTip="Phone Number is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator ID="RegularExpressionValidatorPhoneNumber" runat="server" 
                        ControlToValidate="PhoneNumber" ToolTip="Please enter in a phone number (xxx-xxx-xxxx)" ErrorMessage="Please enter a valid phone number (xxx-xxx-xxxx)" 
                        ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}" ValidationGroup="CreateUserWizard1">*</asp:RegularExpressionValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</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"&g开发者_如何学Ct;</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>
                                <asp:RegularExpressionValidator ID="EmailCorrectFormat" runat="server" 
                        ControlToValidate="Email" ToolTip="E-mail in a correct format is required." ErrorMessage=" Please enter a valid email (Ex. bob@gmail.com)" 
                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="CreateUserWizard1">*</asp:RegularExpressionValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Security Question:</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">Security Answer:</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="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>
                </table>

                            <asp:SqlDataSource ID="ExtraUserInfo" runat="server" ConnectionString="LoginSQL"
         InsertCommand="INSERT INTO [ExtraUserInfo] ([CompanyName]) VALUES (@CompanyName)"
         ProviderName="MySqlLoginProvider">
        <InsertParameters>
            <asp:ControlParameter Name="CompanyName" Type="String" ControlID="CompanyName" PropertyName="Text" />
        </InsertParameters>
    </asp:SqlDataSource> 

            </ContentTemplate>



        </asp:CreateUserWizardStep>
        <asp:CompleteWizardStep runat="server" />
    </WizardSteps>
</asp:CreateUserWizard>


In code behind follow this: i got it from here{https://web.archive.org/web/20211020103243/https://www.4guysfromrolla.com/articles/070506-1.aspx}

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    TextBox UserNameTextBox =  (TextBox)CreateUserWizardStep2.ContentTemplateContainer.FindControl("UserName");
    SqlDataSource DataSource = (SqlDataSource)CreateUserWizardStep2.ContentTemplateContainer.FindControl("InsertExtraInfo");

    MembershipUser User = Membership.GetUser(UserNameTextBox.Text);
    object UserGUID = User.ProviderUserKey;

    DataSource.InsertParameters.Add("UserId", UserGUID.ToString());
    DataSource.Insert();
} 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜