我正在使用SqlDataSource綁定DetailsView for userprofile,它允許用戶查看其信息並同時更改其詳細信息。 (選擇,檢索和更新)在選定的數據源中找不到名稱爲'Username'的字段或屬性
但是,發生此錯誤 - 當我單擊指向「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 [email protected]"
onselecting="UserProfileDataSource_Selecting"
UpdateCommand="UPDATE UserProfiles SET
Username = @Username,
HomeTown = @HomeTown,
HomepageUrl = @HomepageUrl,
Signature = @Signature WHERE UserId = @UserId ">
但錯誤依然出現: 字段或屬性名稱爲「用戶名」未選擇的數據源
是因爲用戶名是一個外鍵,這就是爲什麼它觸發這個錯誤? hometown,homepageurl和簽名都屬於一個名爲「UserProfiles」的同一張表。而用戶名來自與登錄控件一起創建的aspnet_membership表。 – user1467175
@ user1467175 - 如果要顯示或使用其他表中的數據,則必須將兩個表連接在一起。由於您沒有將aspnet_membership表作爲SELECT的一部分,因此無法訪問它的任何數據。 – DaveB
所以它應該像SELECT [m.UserId],[HomeTown],[HomepageUrl],[Signature] FROM [aspnet_Membership m] INNER JOIN [UserProfiles u] ON m.UserId = u.UserId? – user1467175