I am having trouble displaying an object field of one of my entities in an asp.net application. DataBinder.Eval returns Nothing for some reason.

I have 2 classes as follows:

Public Class Bench
    <ScaffoldColumn(False), StringLength(50), Key> _
    Public Property Code() As String

    <Required> _
    Public Property Type() As BenchType

    <Required, StringLength(50)> _
    Public Property Name() As String

    ...
End Class

Public Class BenchType
    <ScaffoldColumn(False), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)> _
    Public Property ID() As Integer

    <Required, StringLength(50)> _
    Public Property Name() As String

    ...
End Class

I am trying to display the Bench entities in a GridView as follows (with a properly implemented ObjectDataSource odsBenches):

<asp:GridView ID="grwBenches" runat="server" DataSourceID="odsBenches"
    DataKeyNames="Code,Type,Name" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Code">
            <ItemTemplate>
                <asp:Label id="lblCode" runat="server" Text='<%# Bind("Code")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label id="lblName" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Bench Type">
            <ItemTemplate>
                <asp:Label id="lblBenchType" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Type.Name")%>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code and Name fields are displayed ok, but the Type field is shown as empty, this is the resulting markup from the rendered page:

<table cellspacing="0" rules="all" border="1" id="MainContent_grwBenches" style="border-collapse:collapse;">
    <tr>
        <th scope="col">Code</th><th scope="col">Name</th><th scope="col">Bench Type</th>
    </tr><tr>
        <td>
                <span id="MainContent_grwBenches_lblCode_0">OK Code</span>
            </td><td>
                <span id="MainContent_grwBenches_lblName_0">OK Name</span>
            </td><td>
                <span id="MainContent_grwBenches_lblBenchType_0"></span>
            </td>
    </tr>
</table>

When I step through the code, It appears that <%# DataBinder.Eval(Container.DataItem, "Type.Name")%> returns Nothing but when I check the database, the Type field contains the type code.

What could be the problem?

Related posts

Recent Viewed