0

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 Answer 1

1

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";
Sign up to request clarification or add additional context in comments.

2 Comments

Great it works, thanks, but frankly, I don't understand why we aren't allow to select specificly CityID or CityName, isn't it a performance issue to retrieve all table instead of just the necessary fields? On the other hand we are allow to select specific fields of Medical table
You can select the CityName directly but then you'd have to change the binding to just CityName rather than City.CityName. This indeed can be better in terms of performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.