0
votes

My database relationship is as follow;

Country -> Region -> City -> Medical -> MedicalServices all of them are connected to each other as one to many.

Here is the listview and entitydatasource which works as intended without any problem. The problem is with the code behind.

 <asp:ListView ID="lvMainContent" runat="server" 
    DataKeyNames="MedicalID">
<LayoutTemplate>
    <ul ID="itemPlaceholderContainer" runat="server" 
        style="font-family: Verdana, Arial, Helvetica, sans-serif;">
        <li runat="server" id="itemPlaceholder" />
    </ul>
<div style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;"> 
</div>
</LayoutTemplate>
    <ItemSeparatorTemplate>
        <br />
    </ItemSeparatorTemplate>
<ItemTemplate>
    <li style="background-color: #DCDCDC;color: #000000;">Medicals:
       <br />
        MedicalName:
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("medicalName") %>' />
        <br />

        <br />
        CityName:
        <asp:Label ID="Label3" runat="server" Text='<%# Eval("City.CityName") %>' />
        <br />
        <br />
    </li>

</ItemTemplate>

</asp:ListView>
<asp:EntityDataSource ID="lvMainContentDataSource" runat="server"
ConnectionString="name=EntitiesMedical" 
    DefaultContainerName="EntitiesMedical"  EntitySetName="Medicals"  

    EnableFlattening="False">

</asp:EntityDataSource>

And Here is the Code Behind which is connected to "Search" button actually Code Behind and ASP side seem identical to me but when I press the Search button an error is thrown, I add it after the code behind.

protected void SearchButton_Click(object sender, EventArgs e)
    {          

       using(Entity.EntitiesMedical em = new Entity.EntitiesMedical())
       {

           var result = from m in em.Medicals

                        where m.City.CityName == "Düsseldorf"

                        select new
                        {
                           m.MedicalID,
                           m.medicalName,
                           m.City.CityName
                        };

           EntityDataSource eds = new EntityDataSource();
           eds.ConnectionString = "name=EntitiesMedical";
           eds.DefaultContainerName = "EntitiesMedical";
           eds.EntitySetName = "Medicals";

           lvMainContent.DataSource = result.ToList();
           lvMainContent.DataBind();

       }          

    }

DataBinding: '<>f__AnonymousType5`3[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'City'.

Any help would be appreciated. I am confused with Include property actually I don't need it at all I guess.

1

1 Answers

1
votes

Your code behind query returns anonymous type with

City.CityName

On the other hand, the list view binds to

City.CityName

This won't work, the returned property is a string already and the list expects an object, city.

Just do:

                    select new
                    {
                       m.MedicalID,
                       m.medicalName,
                       m.City
                    };

       EntityDataSource eds = new EntityDataSource();
       eds.ConnectionString = "name=EntitiesMedical";
       eds.DefaultContainerName = "EntitiesMedical";
       eds.EntitySetName = "Medicals";