2

ログインページがありました。ユーザーが正常にログインすると、プロファイル/情報を表示および管理できます。これは、データベースからデータを取得し、フォームビューに表示することによって行われます。

ただし、次のエラーが 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:     }

ユーザープロファイル.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>

ユーザープロファイル.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

4 に答える 4

7

新しい SqlParameter を作成し、コレクションに追加します。

SqlParameter param = new SqlParameter("@UserId", currentUserId);
e.Command.Parameters.Add(param);
于 2012-06-30T17:17:20.890 に答える
5

次のことを試してください。

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

私はこれをテストしませんでしたが

于 2012-06-30T17:45:45.600 に答える
0

SqlDataSource にパラメーターが既にリストされている場合は、それをポイントして値を変更するだけです...

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

ASP ページにリストしていない限り、パラメータを作成する必要はありません。

于 2014-05-16T05:23:47.993 に答える
0

パラメータを追加する必要があります

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

追加したら、例のようにインデクサーを介してアクセスできます。


アップデート

System.Data.Common名前空間を使用している場合、AddWithValueメソッドは使用できません。次のようなことをする必要があります

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

これは少し複雑ですが、SqlParamterやのような特定の型のパラメーターを暗黙的に作成する必要がないという利点がありますOleDbParameter

于 2012-06-30T16:44:37.117 に答える