2012-06-29 20 views
2

我正在使用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 "> 

但錯誤依然出現: 字段或屬性名稱爲「用戶名」未選擇的數據源

回答

4

檢查中的UserProfiles表中的字段名稱上找到。看起來你沒有用戶名字段。明確聲明您希望選擇的字段而不是使用SELECT * FROM ..語法是一種很好的做法。

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

是因爲用戶名是一個外鍵,這就是爲什麼它觸發這個錯誤? hometown,homepageurl和簽名都屬於一個名爲「UserProfiles」的同一張表。而用戶名來自與登錄控件一起創建的aspnet_membership表。 – user1467175

+0

@ user1467175 - 如果要顯示或使用其他表中的數據,則必須將兩個表連接在一起。由於您沒有將aspnet_membership表作爲SELECT的一部分,因此無法訪問它的任何數據。 – DaveB

+0

所以它應該像SELECT [m.UserId],[HomeTown],[HomepageUrl],[Signature] FROM [aspnet_Membership m] INNER JOIN [UserProfiles u] ON m.UserId = u.UserId? – user1467175

1

你可以嘗試重新排序字段,看看它是否只是用戶名的問題嗎?即

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

如果數據源出現問題,你現在應該在'HomeTown'上得到異常。如果你這樣做,有一兩件事,試圖將是從參數分配移除@:

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

編輯

  1. 在你的SelectCommand,你別:看到你修改的SQL,兩件事情後,似乎沒有選擇用戶名。如果[aspnet_Membership].UserId是您的用戶名,您可以SELECT [aspnet_Membership].UserId AS Username
  2. 在您的UpdateCommand中,即使它在UserProfiles表中不存在,也是爲用戶名分配一個值。你也需要使用JOIN。
+0

嗨,代碼工作正常,如果我沒有添加在用戶名字段只有在我添加在用戶名字段,然後出現此錯誤。 – user1467175

+0

好的,根據你對DaveB的迴應,這聽起來像你有不同的問題。我會第二次建議顯式選擇你想要的字段,而不是使用*通配符。然後你的錯誤將顯示爲源自SQL語句。 –

相關問題