views:

55

answers:

2

Hello, I am using entity framework. My model has entity tblGameInfo where in navigation properties are associations called tblPlayer1 and tblPlayer2 which links to entity tblPlayer. I have listview on my page where I want to show game info with players names. I have found that in of listview I should have e.g. one of those:

                <td>
                    <asp:Label ID="tblPlayer1Label" runat="server" 
                        Text='<%# Eval("tblPlayer1.FirstName")%>' />
                </td>
                <td>
                    <asp:Label ID="tblPlayer2Label" runat="server" 
                        Text='<%# GetPlayerName(Eval("tblPlayer2.id")) %>' />
                </td>

But it does not work. I got nothing displayed in a listview. I have tried many different options but cannot find working one. Probably is some simple mistake but I am new to asp.net and cannot find it. Second problem with this is editing. In edit template there is dropdownlist with all players.

                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" 
                       DataTextField="FullName" 
                       DataValueField="id" 
                       SelectedValue='<%# Bind("tblPlayer2") %>'>  <- ??
                    </asp:DropDownList>
                </td>

How do I databind it? It is not allowed to bind and change properties? It writes me that element does not exist on a list of elements.

A: 

Try the following:

Text='<%# GetPlayerName(((MyObjectType)Eval(("tblPlayer2")).id) %>' />

There may be an extra ) incorrectly there, but essentially cast the player 2 reference and then get its ID.

Also, with EF, you have to load all related objects before you can use them, so make sure the object being bound has its navigation properties loaded.

Lastly, for the drp down list,you have to cast the reference and refer to the ID of the player, which you can get from my updated sample.

HTH.

Brian
I think I need some more explanations. I cannot make it work. IF I paste e.g. tblPlayer (which is the name of the entity that contains id) for MyObjectType than error occurs that tblPlayer does not exist. So how should I cast it? to what object? How can I check if bound object has navigation property loaded?
piotrmichal
Whatever type the object is; I don't know the class name so I put up a sample. All navigation properties, with the Player1Reference property of the main entity, have an IsLoaded property and a Load() method that must be called in advance, or when you query, you need to use the Include() method to include the relationships. I know its a pain, but unfortunately, that's what it wants.
Brian
A: 

To your question 1:

I guess that you are using an EntityDataSource as datasource for your ListView, is that right?

If so, try to add the following attribute to your EntityDataSource:

Include="tblPlayer1, tblPlayer2"

This loads the related Entities when the DataSource executes the query.

To your question 2:

Here it is important to know if you are using Entity Framework version 4 or version 1. And in case you are using EF4 if you include foreign keys into your model or not.

If you include foreign keys, then you should have scalar properties like tblPlayer1ID and tblPlayer1ID or similar in your tblGameInfo entity which represent your foreign key columns in the underlying database table. In this case Bind("tblPlayer2ID") in your DropDownList should work.

Slauma
Yes, Include helped. Thanks
piotrmichal