0

我一直在按照教程在此处的“创建用户向导”中创建其他字段:

http://www.4guysfromrolla.com/articles/070506-1.aspx#postadlink

我收到一个错误,即 Object reference not set to an instance on an object on my line DataSource.InsertParameters.Add("UserId", UserGUID.ToString());

我的 ASP 代码是这样的:

    <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" oncreateduser="CreateUserWizard1_CreatedUser">
    <WizardSteps>
        <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
            <ContentTemplate>
                <table>
                    <tr>
                        <td class="text-center" colspan="2">
                            Sign Up for Your New Account
                        </td>
                    </tr>
                    <tr>
                        <td class="text-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 class="text-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 class="text-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>
                        <td class="text-center">
                            <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 class="text-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 class="text-right">
                            <asp:Label ID="ManagerFirstNameLabel" runat="server" AssociatedControlID="ManagerFirstName">First Name:</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="ManagerFirstName" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="ManagerFirstNameRequired" runat="server" ControlToValidate="ManagerFirstName"
                                ErrorMessage="First Name is required." ToolTip="First Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right">
                            <asp:Label ID="ManagerSurnameLabel" runat="server" AssociatedControlID="ManagerSurname">Surname:</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="ManagerSurname" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="ManagerSurnamRequired" runat="server" ControlToValidate="ManagerSurname"
                                ErrorMessage="Surname is required." ToolTip="Surname is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right">
                            <asp:Label ID="TeamNameLabel" runat="server" AssociatedControlID="TeamName">Team Name:</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="TeamName" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="TeamNameRequired" runat="server" ControlToValidate="TeamName"
                                ErrorMessage="Team Name is required." ToolTip="Team Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right">
                            <asp:Label ID="TeamStadiumLabel" runat="server" AssociatedControlID="TeamStadium">Team Stadium Name:</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="TeamStadium" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="TeamStadiumRequired" runat="server" ControlToValidate="TeamStadium"
                                ErrorMessage="Team Stadium Name is required." ToolTip="Team Stadium Name is required."
                                ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    </tr>
                    <tr>
                        <td class="text-right">
                            <asp:Label ID="RecieveReminderLabel" runat="server" AssociatedControlID="RecieveReminder">Recieve Reminder Email:</asp:Label>
                        </td>
                        <td>
                            <asp:CheckBox ID="RecieveReminder" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right">
                            <asp:Label ID="RecieveSummaryLabel" runat="server" AssociatedControlID="RecieveSummary">Recieve Summary Email:</asp:Label>
                        </td>
                        <td>
                            <asp:CheckBox ID="RecieveSummary" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center" colspan="2" style="color: Red;">
                            <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center" colspan="2" style="color: Red;">
                            <asp:Literal ID="Literal1" runat="server" EnableViewState="False"></asp:Literal>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:CreateUserWizardStep>
        <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" />
    </WizardSteps>
</asp:CreateUserWizard>

<asp:SqlDataSource ID="InsertAdditionalUserDetails" runat="server" ConnectionString="<%$ ConnectionStrings:RaiseFantasyLeagueConnectionString %>"
    InsertCommand="INSERT INTO [UserDetails] ([UserId], [ManagerFirstName], [ManagerSurname], [TeamName], [TeamStadium], [RecieveReminder], [RecieveSummary]) VALUES (@UserId, @ManagerFirstName, @ManagerSurname, @TeamName, @TeamStadium, @RecieveReminder, @RecieveSummary)" ProviderName="System.Data.SqlClient">
    <InsertParameters>
        <asp:ControlParameter Name="ManagerFirstName" Type="String" ControlID="ManagerFirstName" PropertyName="Text" />
        <asp:ControlParameter Name="ManagerSurname" Type="String" ControlID="ManagerSurname" PropertyName="Text" />
        <asp:ControlParameter Name="TeamName" Type="String" ControlID="TeamName" PropertyName="Text" />
        <asp:ControlParameter Name="TeamStadium" Type="String" ControlID="TeamStadium" PropertyName="Text" />
        <asp:ControlParameter Name= "RecieveReminder" Type="Boolean" ControlID="RecieveReminder" PropertyName="Checked" />
        <asp:ControlParameter Name= "RecieveSummary" Type="Boolean" ControlID="RecieveSummary" PropertyName="Checked" />
    </InsertParameters>
</asp:SqlDataSource>

而C#代码是这样的

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

        MembershipUser User = Membership.GetUser(UserNameTextBox.Text);

        if (User != null)
        {
            object UserGUID = User.ProviderUserKey;
            DataSource.InsertParameters.Add("UserId", UserGUID.ToString());
            DataSource.Insert();
        }
    }

任何帮助,将不胜感激 !

4

3 回答 3

0

我认为这将解决您的问题

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    MembershipUser User = Membership.GetUser(CreateUserWizard1.UserName);
    object userGUID = User.ProviderUserKey;
    TextBox firstname = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FirstName");
    TextBox lastname = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("LastName");
    TextBox companyname = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("CompanyName");
    TextBox address1 = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Address1");
    TextBox address2 = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Address2");
    TextBox city = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("City");
    DropDownList state = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("State");
    TextBox zip = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Zip");
    TextBox phone_number = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Phone");
    TextBox email_address = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Email");


    SqlDataSource1.InsertParameters.Add("userid", userGUID.ToString());
    SqlDataSource1.InsertParameters.Add("firstname", firstname.Text);
    SqlDataSource1.InsertParameters.Add("lastname", lastname.Text);
    SqlDataSource1.InsertParameters.Add("companyname", companyname.Text);
    SqlDataSource1.InsertParameters.Add("address1", address1.Text);
    SqlDataSource1.InsertParameters.Add("address2", address2.Text);
    SqlDataSource1.InsertParameters.Add("city", city.Text);
    SqlDataSource1.InsertParameters.Add("state", state.SelectedValue);
    SqlDataSource1.InsertParameters.Add("zip", zip.Text);
    SqlDataSource1.InsertParameters.Add("phone_number", phone_number.Text);
    SqlDataSource1.InsertParameters.Add("email_address", email_address.Text);
    SqlDataSource1.Insert();
}
于 2013-03-01T20:24:42.930 回答
0

您的“DataSource”或“UserGUID”为 NULL。

于 2013-03-01T12:41:22.800 回答
0

你得到这个错误是因为你试图将对象转换为字符串。试试这个它可能对你有帮助

而不是对象使用 var var user

于 2013-03-01T12:42:20.723 回答