c# - 在所选数据源中找不到名称为 'Username' 的字段或属性

标签 c# asp.net sql-server

我正在使用 SqlDataSource 为用户配置文件绑定(bind) 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 ">

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

最佳答案

检查 UserProfiles 表中的字段名称。您似乎没有用户名字段。最好明确声明要 SELECT 的字段,而不是使用 SELECT * FROM .. 语法。

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

关于c# - 在所选数据源中找不到名称为 'Username' 的字段或属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11265203/

相关文章:

c# - 您如何动态分配母版页?

c# - 在sql server 2005、2008、2012上使用smo进行数据库收集

sql-server - 从SQL Server触发器调用Powershell脚本

mysql - 如何获得链接到第三个表的两个表的总和

c# - Razor 页面 : Does OnGet Handler "accept" a request body?

c# - 反射 - 检查所有可为空的属性是否有值

c# - 如何以一定的延迟启动 MS TPL 的任务类实例?

c# - 扩展方法(类)或访问者模式

c# - 主机不允许连接到此MySQL服务器错误,但我没有使用MySQL

asp.net - 通过子域 ASP.NET 进行表单例份验证