2

我正在使用 SqlDataSource 为 userprofile 绑定 DetailsView,它允许用户查看他们的信息并同时更改他们的详细信息。(选择、检索和更新)

但是,发生了这个错误——当我单击超链接指向“用户配置文件”页面时,在所选数据源上找不到名为“用户名”的字段或属性。

Exception Details: System.Web.HttpException: A field or property with the name 'Username' was not found on the selected data source.

Source Error: 

An unhandled exception was generated during the execution of the current web request.   Information regarding the origin and location of the exception can be identified using the   exception stack trace below.

这是 userprofile.aspx 文件中的代码:

  <asp:DetailsView ID="UserProfile" runat="server" 
        AutoGenerateRows="False" DataKeyNames="UserId" 
        DataSourceID="UserProfileDataSource" DefaultMode="Edit" 
        onitemupdated="UserProfile_ItemUpdated">
        <Fields>
           <asp:BoundField DataField="Username" HeaderText="Username" 
                SortExpression="Username" />
            <asp:BoundField DataField="HomeTown" HeaderText="HomeTown" 
                SortExpression="HomeTown" />
            <asp:BoundField DataField="HomepageUrl" HeaderText="HomepageUrl" 
                SortExpression="HomepageUrl" />
            <asp:BoundField DataField="Signature" HeaderText="Signature" 
                SortExpression="Signature" />
            <asp:CommandField ShowEditButton="True" />
        </Fields>
    </asp:DetailsView>

    <asp:SqlDataSource ID="UserProfileDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SecurityTutorialsConnectionString %>" 

        SelectCommand="SELECT * FROM [UserProfiles] WHERE ([UserId] = @UserId)" 
        onselecting="UserProfileDataSource_Selecting" 
        UpdateCommand="UPDATE UserProfiles SET
        Username = @Username,
        HomeTown = @HomeTown,
        HomepageUrl = @HomepageUrl,
        Signature = @Signature   WHERE UserId = @UserId ">

        <SelectParameters>
        <asp:Parameter Name="UserId" Type="Object" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="HomeTown" />
            <asp:Parameter Name="HomepageUrl" />
            <asp:Parameter Name="Signature" />
            <asp:Parameter Name="UserId" />
        </UpdateParameters>
    </asp:SqlDataSource>

这是 userprofile.aspx.cs 背后的代码:

 protected void UserProfileDataSource_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;
    }

编辑 我已将 SQL 查询更改为:

 SelectCommand= "SELECT [aspnet_Membership].UserId, [HomeTown], [HomepageUrl], [Signature] FROM [UserProfiles] INNER JOIN [aspnet_Membership] ON aspnet_Membership.UserId = UserProfiles.UserId WHERE aspnet_Membership.UserId=@UserId"
    onselecting="UserProfileDataSource_Selecting"
    UpdateCommand="UPDATE UserProfiles SET
    Username = @Username,
    HomeTown = @HomeTown,
    HomepageUrl = @HomepageUrl,
    Signature = @Signature   WHERE UserId = @UserId ">

但错误仍然出现:在所选数据源上找不到名为“用户名”的字段或属性

4

2 回答 2

4

检查 UserProfiles 表中的字段名称。看来您没有用户名字段。明确声明要选择的字段而不是使用SELECT * FROM ..语法是一种很好的做法。

 <asp:BoundField DataField="Username" HeaderText="Username"  
                SortExpression="Username" /> 
于 2012-06-29T16:11:11.823 回答
1

您可以尝试重新排序字段以查看是否只是用户名的问题?IE,

<asp:BoundField DataField="HomeTown" HeaderText="HomeTown" 
                SortExpression="HomeTown" />
<asp:BoundField DataField="Username" HeaderText="Username" 
                SortExpression="Username" />

如果数据源有问题,您现在应该在“HomeTown”上获得异常。如果这样做,可以尝试的一件事是从参数分配中删除 @:

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

编辑:看到修改后的 SQL 后,有两件事:

  1. 在您的 SelectCommand 中,您似乎没有选择用户名。如果[aspnet_Membership].UserId是您的用户名,您可以SELECT [aspnet_Membership].UserId AS Username
  2. 在您的 UpdateCommand 中,您正在为 Username 分配一个值,即使它在 UserProfiles 表中不存在。您还需要在那里使用 JOIN 。
于 2012-06-29T16:12:43.710 回答