我使用SqlDataSource绑定DetailsView for userprofile,允许用户查看他们的信息,同时更改他们的详细信息。 (选择,检索和更新)
然而,发生此错误 - 名称为'用户名'的字段或属性单击超链接以指向" userprofile"在所选数据源上找不到。页。
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 ">
但错误仍然出现: 名称为&#39;用户名&#39;的字段或媒体资源未在所选数据源中找到
答案 0 :(得分:4)
检查UserProfiles表中的字段名称。您似乎没有用户名字段。最好明确说明要SELECT的字段,而不是使用SELECT * FROM ..
语法。
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
答案 1 :(得分:1)
您可以尝试重新排序字段,看看它是否只是用户名问题?即,
<asp:BoundField DataField="HomeTown" HeaderText="HomeTown"
SortExpression="HomeTown" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
如果数据源出现问题,您现在应该在'HomeTown'上获得例外。如果你这样做,一件事就是从参数赋值中删除@:
e.Command.Parameters["UserId"].Value = currentUserId;
编辑:看到修改后的SQL后,有两件事:
[aspnet_Membership].UserId
是您的用户名,则可以SELECT [aspnet_Membership].UserId AS Username