C# 此 SqlParameterCollection 不包含 ParameterName 为“@UserId”的 SqlParameter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11275807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:14:09  来源:igfitidea点击:

An SqlParameter with ParameterName '@UserId' is not contained by this SqlParameterCollection

c#sqlmysql-error-1064

提问by user1467175

I had a login page. once user successfuly logged in, they can view and manage their profile/information. This would be done by retrieving data from database and display on a formview.

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

However this following error appeared inside my userprofile.aspx.cs file:

但是,以下错误出现在我的 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:

用户配置文件.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:

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;

    }

采纳答案by codingbiz

Try the following:

请尝试以下操作:

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

I didn't test this though

虽然我没有测试这个

回答by Olivier Jacot-Descombes

You must add the parameter with

您必须添加参数

e.Command.Parameters.AddWithValue("@UserId", currentUserId);

Once you have added it you could access it through the indexer as in your example.

添加它后,您可以像示例中一样通过索引器访问它。



UPDATE

更新

If you are working with the System.Data.Commonnamespace, the AddWithValuemethod is not available. You will have to do something like

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

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

This is a little bit more complicated but has the advantage that you do not have to implicitly create a parameter of a specific type like SqlParamteror OleDbParameter.

这有点复杂,但优点是您不必隐式创建特定类型的参数,例如SqlParamteror OleDbParameter

回答by Lee O.

Create a new SqlParameter and add it to the collection.

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

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

回答by Fandango68

If you already have the parameter listed in your SqlDataSource, then simply point to it and change it's value ...

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

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

You do not need to create a parameter, unless you have not listed it back in your ASP page.

您不需要创建参数,除非您没有在 ASP 页中重新列出它。