此SqlParameterCollection不包含带有ParameterName'@ UserId'的SqlParameter

时间:2012-06-30 16:40:38

标签: c# sql mysql-error-1064

我有一个登录页面。一旦用户成功登录,他们就可以查看和管理他们的个人资料/信息。这可以通过从数据库中检索数据并在formview上显示来完成。

但是我的userprofile.aspx.cs文件中出现了以下错误:

Exception Details: System.IndexOutOfRangeException: An SqlParameter with ParameterName '@UserId' is not contained by this SqlParameterCollection.

Source Error: 

Line 44: 
Line 45:         // Assign the currently logged on user's UserId to the @UserId parameter
Line 46:         e.Command.Parameters["@UserId"].Value = currentUserId;
Line 47: 
Line 48:     }

Userprofile.aspx:

<asp:FormView ID="FormView1" runat="server" 
            DataSourceID="SqlDataSource1" DataKeyNames="UserId">
            <EditItemTemplate>
                UserId:
                <asp:Label ID="UserIdLabel1" runat="server" Text='<%# Eval("UserId") %>' />
                <br />
                Password:
                <asp:TextBox ID="PasswordTextBox" runat="server" 
                    Text='<%# Bind("Password") %>' />
                <br />
                Email:
                <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
                <br />
                HomeTown:
                <asp:TextBox ID="HomeTownTextBox" runat="server" 
                    Text='<%# Bind("HomeTown") %>' />
                <br />
                HomepageUrl:
                <asp:TextBox ID="HomepageUrlTextBox" runat="server" 
                    Text='<%# Bind("HomepageUrl") %>' />
                <br />
                Signature:
                <asp:TextBox ID="SignatureTextBox" runat="server" 
                    Text='<%# Bind("Signature") %>' />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                    CommandName="Update" Text="Update" />
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
            <InsertItemTemplate>
                UserId:
                <asp:TextBox ID="UserIdTextBox" runat="server" Text='<%# Bind("UserId") %>' />
                <br />
                Password:
                <asp:TextBox ID="PasswordTextBox" runat="server" 
                    Text='<%# Bind("Password") %>' />
                <br />
                Email:
                <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
                <br />
                HomeTown:
                <asp:TextBox ID="HomeTownTextBox" runat="server" 
                    Text='<%# Bind("HomeTown") %>' />
                <br />
                HomepageUrl:
                <asp:TextBox ID="HomepageUrlTextBox" runat="server" 
                    Text='<%# Bind("HomepageUrl") %>' />
                <br />
                Signature:
                <asp:TextBox ID="SignatureTextBox" runat="server" 
                    Text='<%# Bind("Signature") %>' />
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                    CommandName="Insert" Text="Insert" />
                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </InsertItemTemplate>
            <ItemTemplate>
                UserId:
                <asp:Label ID="UserIdLabel" runat="server" Text='<%# Eval("UserId") %>' />
                <br />
                Password:
                <asp:Label ID="PasswordLabel" runat="server" Text='<%# Bind("Password") %>' />
                <br />
                Email:
                <asp:Label ID="EmailLabel" runat="server" Text='<%# Bind("Email") %>' />
                <br />
                HomeTown:
                <asp:Label ID="HomeTownLabel" runat="server" Text='<%# Bind("HomeTown") %>' />
                <br />
                HomepageUrl:
                <asp:Label ID="HomepageUrlLabel" runat="server" 
                    Text='<%# Bind("HomepageUrl") %>' />
                <br />
                Signature:
                <asp:Label ID="SignatureLabel" runat="server" Text='<%# Bind("Signature") %>' />
                <br />

            </ItemTemplate>
        </asp:FormView>
    </p>
<p>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SecurityTutorialsConnectionString %>" 
            onselecting="SqlDataSource1_Selecting" 
            SelectCommand="SELECT UserProfiles.UserId, aspnet_Membership.Password, aspnet_Membership.Email, UserProfiles.HomeTown, UserProfiles.HomepageUrl, UserProfiles.Signature FROM aspnet_Membership INNER JOIN UserProfiles ON aspnet_Membership.UserId = UserProfiles.UserId">
        </asp:SqlDataSource>
    </p>
<p>
        &nbsp;</p>

</asp:Content>

Userprofile.aspx.cs:

 protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        // Get a reference to the currently logged on user
        MembershipUser currentUser = Membership.GetUser();

        // Determine the currently logged on user's UserId value
        Guid currentUserId = (Guid)currentUser.ProviderUserKey;

        // Assign the currently logged on user's UserId to the @UserId parameter
        e.Command.Parameters["@UserId"].Value = currentUserId;

    }

4 个答案:

答案 0 :(得分:7)

创建一个新的SqlParameter并将其添加到集合中。

SqlParameter param = new SqlParameter("@UserId", currentUserId);
e.Command.Parameters.Add(param);

答案 1 :(得分:4)

尝试以下方法:

DbParameter param = e.Command.CreateParameter();
param.ParameterName = "@UserId";
param.Value = currentUserId;
e.Command.Parameters.Add(param);

我没试过这个

答案 2 :(得分:0)

您必须使用

添加参数
e.Command.Parameters.AddWithValue("@UserId", currentUserId);

添加后,您可以通过索引器访问它,如示例所示。


更新

如果您正在使用System.Data.Common命名空间,则AddWithValue方法不可用。你必须做类似的事情

var param = e.Command.CreateParameter("@UserId", currentUserId);
e.Command.Parameters.Add(param);

这有点复杂,但其优势在于您无需隐式创建特定类型的参数,例如SqlParamterOleDbParameter

答案 3 :(得分:0)

如果您已经在SqlDataSource中列出了参数,那么只需指向它并更改它的值......

e.Command.Parameters["@UserId"].Value = currentUserId;

除非您没有在ASP页面中列出参数,否则无需创建参数。