6

I have an Entity Data Model with two entities in it "Roles" and "Users". There is a navigation property from I have a EntityDataSource and a GridView. The EntityDataSource points to the Users entity and has an Include="Roles" parameter.

I've added a BoundField in the GridView that points to RoleName, a property of the entity Roles. However, when I execute the code I get the above error.

I have used very similar code successfully in another application. Any ideas why this isn't working?

Here is my EntityDataSource:

    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=pbu_checklistEntities" 
    DefaultContainerName="pbu_checklistEntities" EnableDelete="True" 
    EnableFlattening="False" EnableUpdate="True" EntitySetName="Users" Include="Role">
    </asp:EntityDataSource>

And here is the BoundField:

<asp:BoundField DataField="RoleName" HeaderText="Role" SortExpression="RoleName" />
Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
  • I guess the "RoleName" is not right spelled as the property name in your datasource .. – Akram Shahda Apr 15 '11 at 18:14
  • No, it is definitely named that in both the underlying datasource (MSSQL) and in the entity data model. – Dave Mackey Apr 15 '11 at 18:16
  • In Roles the columns are ID, RoleName, RoleDescription, EditPermissions, EditBS, EditLibrary, EditSecurity, EditFinAid, EditStudentLife, EditStudentMin, EditRegistrar. In Users ID, UserID, RoleID – Dave Mackey Apr 15 '11 at 18:31
  • Despite of my answer I am confused now: Is it `Role` (single reference) or `Roles` (collection) in the `User` entity? And what is `RoleName` then? Shouldn't it be `Role.Name` or `Role.RoleName`? Or do you have a `Select` statement in the EntityDataSource which projects `Role.Name` to `RoleName` as an alias? – Slauma Apr 15 '11 at 18:36

1 Answers1

20

You cannot use an asp:BoundField for a property of a related navigation property. You can only use an asp:TemplateField and then bind it as readonly with Eval (not Bind). BoundFields are always using Bind internally, that's the reason why it doesn't work. I had to figure this out myself some time ago:

Columns of two related database tables in one ASP.NET GridView with EntityDataSource

Community
  • 1
  • 1
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • Thanks! That is a great help. – Dave Mackey Apr 15 '11 at 18:50
  • The linked question addresses the issue more generally but +1 for this question and answer since it came up in my search--the question is more directly related to the answer. – pseudocoder Aug 15 '12 at 15:53
  • 2
    This only seems to be true in IIS 6 and not IIS 7. Can anyone confirm or deny this difference? – John Bledsoe Sep 26 '13 at 20:59
  • 2
    Yes John Bledsoe, I only ran in to this issue when trying to deploy a project I had developed on VS 2013/IIS 7 on a VS 2010/IIS 6 setup. I had no issues when testing in my VS 2013/IIS 7 environment. Converting everything to TemplateFields worked for me. – Emeka Mar 05 '14 at 16:38