-1

当我点击 CreateUserWizard 控件的“下一步”按钮时出现错误 - “对象引用未设置为对象的实例”。我检查了我的代码,我没有将任何变量留空。但我仍然可以'不解决这个错误。

 Source Error: 

    Line 29:         object UserGUID = User.ProviderUserKey;
    Line 30: 
    Line 31:         DataSource.InsertParameters.Add("UserId", UserGUID.ToString()); //this is the line that caused the error
    Line 32: 
    Line 33:         DataSource.Insert();

This is my html code file: 

 <form id="form1" runat="server">
    <div>
        <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" 
            OnCreatedUser="CreateUserWizard1_CreatedUser" CreateUserButtonText="Next" 
            FinishCompleteButtonText="Create User" Height="330px" 
            Width="512px">
            <WizardSteps>

                <asp:CreateUserWizardStep ID="CreateUserWizardStep2" runat="server" >
                <ContentTemplate>
                    <table>
                        <tr>
                            <th>User Information</th>
                        </tr>
                        <tr>
                            <td>Username:</td>
                            <td class="style2">
                                <asp:TextBox runat="server" ID="UserName" />
                                <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator9" ControlToValidate="UserName" 
                                    ErrorMessage="Username is required." />
                            </td>
                        </tr>
                        <tr>
                            <td>Password:</td>
                            <td class="style2">
                                <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                                <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator10" ControlToValidate="Password" 
                                    ErrorMessage="Password is required." />
                            </td>
                        </tr>
                        <tr>
                            <td>Confirm Password:</td>
                            <td class="style2">
                                <asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />
                                <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator13" ControlToValidate="ConfirmPassword" 
                                    ErrorMessage="Confirm Password is required." />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Email:</td>
                              <td class="style2">
                                <asp:TextBox runat="server" ID="Email" />
                                  <asp:RequiredFieldValidator ID="RequiredFieldValidator16" runat="server" ControlToValidate="Email" 
                                      ErrorMessage="Email required."></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>Question:</td>
                            <td class="style2">
                                <asp:TextBox runat="server" ID="Question" />
                                <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator12" ControlToValidate="Question" 
                                    ErrorMessage="Question is required." />
                            </td>
                        </tr>
                        <tr>
                            <td>Answer:</td>
                            <td class="style2">
                                <asp:TextBox runat="server" ID="Answer" />
                                <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator14" ControlToValidate="Answer" 
                                    ErrorMessage="Answer is required." />
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                 <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
                                        ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."></asp:CompareValidator>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                    </table>


                </ContentTemplate>
                </asp:CreateUserWizardStep>
                <asp:WizardStep ID="CreateUserWizardStep0" runat="server" Title="User Details">
                    <table>
                        <tr>
                            <th>Billing Information</th>
                        </tr>
                        <tr>
                            <td>Name:</td>
                            <td class="style1">
                                <asp:TextBox runat="server" ID="CustName" />
                                  <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator18" ControlToValidate="CustName"
                                     ErrorMessage="Name is required."  />
                                     </td>
                        </tr> 
                        <tr>
                            <td>Contact Number:</td>
                            <td class="style1">
                                <asp:TextBox runat="server" ID="CustNum"  />
                                <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator19" ControlToValidate="CustNum"
                                     ErrorMessage="Contact num is required."  />
                            </td>
                        </tr>   
                        <tr>
                            <td>Role:</td>
                            <td class="style1">
                                <asp:DropDownList ID="CustRole" runat="server" AutoPostBack="True" Height="16px" 
                                    Width="123px">
                                    <asp:ListItem></asp:ListItem>
                                    <asp:ListItem>Bride</asp:ListItem>
                                    <asp:ListItem>Groom</asp:ListItem>
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator20" ControlToValidate="CustRole"
                                     ErrorMessage="Role is required." />
                            </td>
                        </tr>                
                        <tr>
                            <td>
                                Status:</td>
                            <td class="style1">
                                <asp:DropDownList ID="CustStatus" runat="server" AutoPostBack="True" 
                                    Height="17px" Width="121px">
                                    <asp:ListItem></asp:ListItem>
                                    <asp:ListItem>Status A</asp:ListItem>
                                    <asp:ListItem>Status B</asp:ListItem>
                                    <asp:ListItem>Status C</asp:ListItem>
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator15" runat="server" ControlToValidate="CustStatus"
                                    ErrorMessage="Status is required."/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;</td>
                            <td class="style1">
                                &nbsp;</td>
                        </tr>
                        <tr>
                            <td>
                                Partner&#39;s Name:</td>
                              <td class="style1">
                                <asp:TextBox runat="server" ID="PName"  />
                                <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator5" ControlToValidate="PName"
                                     ErrorMessage="PName is required."  />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Partner Email:</td>
                            <td class="style1">
                                <asp:TextBox runat="server" ID="PEmail"  />

                            </td>
                        </tr>
                        <tr>
                            <td>
                                PRole:
                            </td>
                            <td class="style1">
                                <asp:DropDownList ID="PRole" runat="server" AutoPostBack="True" Height="16px" 
                                    Width="123px">
                                    <asp:ListItem></asp:ListItem>
                                    <asp:ListItem>Bride</asp:ListItem>
                                    <asp:ListItem>Groom</asp:ListItem>
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator7" ControlToValidate="PRole"
                                     ErrorMessage="PRole is required." />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Wed Date:
                            </td>
                            <td class="style1">
                                <cc1:DatePicker ID="WedDate" runat="server" CalendarDate="" 
                                    TextCssClass="" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server" ControlToValidate="WedDate"
                                    ErrorMessage="Wedding date required"/>
                            </td>
                        </tr>
                    </table>
                </asp:WizardStep>

                <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                    <ContentTemplate>
                        <table>
                            <tr>
                                <td align="center" colspan="2">
                                    Complete</td>
                            </tr>
                            <tr>
                                <td>
                                    Your account has been successfully created.</td>
                            </tr>
                            <tr>
                                <td align="right" colspan="2">
                                    <asp:Button ID="ContinueButton" runat="server" CausesValidation="False" 
                                        CommandName="Continue" Text="Continue" ValidationGroup="CreateUserWizard1" />
                                </td>
                            </tr>
                        </table>


                       <asp:SqlDataSource ID="InsertExtraInfo" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>"
                        InsertCommand="INSERT INTO [UserDetails] ([UserId], [CustName], [CustNum], [CustRole], [CustStatus], [PName], [PEmail], [PRole], [WedDate]) VALUES (@UserId, @CustName, @CustNum, @CustRole, @CustStatus, @PName, @PEmail, @PRole, @WedDate)"
                        ProviderName="<%$ ConnectionStrings:ASPNETDBConnectionString1.ProviderName %>">
                        <InsertParameters>
                            <asp:ControlParameter Name="CustName" Type="String" ControlID="CustName" PropertyName="Text" />
                            <asp:ControlParameter Name="CustNum" Type="String" ControlID="CustNum" PropertyName="Text" />
                            <asp:ControlParameter Name="CustRole" Type="String" ControlID="CustRole" PropertyName="Text" />
                            <asp:ControlParameter Name="CustStatus" Type="String" ControlID="CustStatus" PropertyName="Text" />
                            <asp:ControlParameter Name="PName" Type="String" ControlID="PName" PropertyName="Text" />
                            <asp:ControlParameter Name="PEmail" Type="String" ControlID="PEmail" PropertyName="Text" />
                            <asp:ControlParameter Name="PRole" Type="String" ControlID="PRole" PropertyName="Text" />
                            <asp:ControlParameter Name="WedDate" Type="String" ControlID="WedDate" PropertyName="Text" />
                        </InsertParameters>

                    </asp:SqlDataSource>

                    </ContentTemplate>
                </asp:CompleteWizardStep>
            </WizardSteps>

        </asp:CreateUserWizard>
        <br />
    </div>

This is the code behind: 

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web.Security;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Register : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

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

        SqlDataSource DataSource =
        (SqlDataSource)CreateUserWizardStep2.ContentTemplateContainer.FindControl("InsertExtraInfo");

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

        object UserGUID = User.ProviderUserKey;

        DataSource.InsertParameters.Add("UserId", UserGUID.ToString());  //the error lies at here

        DataSource.Insert();

    }
    }
4

1 回答 1

1

我检查了我的代码,我没有让任何变量为空。但我仍然无法解决这个错误。

您尚未检查所有变量,否则不会发生此错误。

您是否检查过您的 DataSource 以确保找到了 SqlDataSource?确保DataSource的InsertParameters属性不为空?UserGUID 确保它不为空?

于 2012-06-19T18:35:02.557 回答