1

I have an entity called Employee that has a Navigation Property called Groups. I have an entity called Group that is related to an Employee and has fields for Name and Description

I have an EntityDataSource to filter by the Employee selected in GridView1, that has Include Groups, its defined like this:

<asp:EntityDataSource ID="GroupsByEmployeeSource" runat="server" ConnectionString="name=SafetyContext" DefaultContainerName="SafetyContext" EnableDelete="True" EnableFlattening="False" EnableInsert="True" EnableUpdate="True" EntitySetName="Employees" Include="Groups" Where="it.[EID] == @EmpID">
    <WhereParameters>
        <asp:ControlParameter Name="EmpID" Type="Int32" ControlID="GridView1" PropertyName="SelectedDataKey.Value" />
    </WhereParameters>
</asp:EntityDataSource>

GridView3 is used to display the Groups that the Employee belongs to. I have it set up like this:

<asp:GridView runat="server" ID="GridView3" DataSourceID="GroupsByEmployeeSource" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="GroupsByEmployee" runat="server" Text='<%#Eval("Groups.Name") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

And every time I select an employee in GridView1 the follow exception is thrown:

System.Web.HttpException: DataBinding: 'System.Collections.Generic.HashSet`1[[SafetyTrackingConceptApplication.DAL.Group, SafetyTrackingConceptApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' does not contain a property with the name 'Name'.

I must be missing something, but the Entity Group definitely has a property called Name. Does anyone have any idea what is wrong here?

SmashCode
  • 4,227
  • 8
  • 38
  • 56
  • Looking at the exception the runtime is trying to bind the collection instead of your entity. Groups is a collection and hence doesn't have a property called Name. You want to bind the collection elements of Groups which is of type Group – Alaa Masoud May 16 '13 at 02:58
  • So how do I proceed? I've read this : http://stackoverflow.com/questions/2284374/columns-of-two-related-database-tables-in-one-asp-net-gridview-with-entitydataso and I'm doing what worked on there. What is the difference here? – SmashCode May 17 '13 at 15:37

1 Answers1

0

Have/had the same problem. The relationship is returning the collection because of the fk (foreign key) relationship in that the Emp can be in multiple groups, which returns a collection as explained in the comment by AMasoud. So you can have the Groups.Count return the number of times the EmpID shows up the group table, but alas you cannot pinpoint a NAME b/c it will be returning the collection.

If the FK is one to one, or many to one (Not one to many) you will have the ability to pull the name.

ransems
  • 641
  • 7
  • 19