Binding 3rd table fields.

Posts   
 
    
ntext
User
Posts: 8
Joined: 11-May-2009
# Posted on: 28-Jun-2009 23:31:04   

Lets say I m working on Northwind like database I have Customers Orders And OrderDetails table.

I'm binding Customers table "collection" to the asp.net gridview.

And one field from orders table by using template field.

<asp:TemplateField HeaderText="OrderId"> <ItemTemplate> <%# Eval("Orders.OrderId")%> </ItemTemplate> </asp:TemplateField>

but I have an field from the OrderDetails table. (field name not important) How can I reach this kind of properties from the 3rd table.

<asp:TemplateField HeaderText="AnyField"> <ItemTemplate> <%# Eval("[u][b]Orders.OrdersDetails.AnyField[/b][/u]")%> </ItemTemplate> </asp:TemplateField>

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 29-Jun-2009 09:37:19   

Lets say I m working on Northwind like database I have Customers Orders And OrderDetails table.

I'm binding Customers table "collection" to the asp.net gridview

Just for the records, your example should be reversed. I mean you should say you are binding OrderDetails and you want to display a field from the Customer.

Because the Customer has many Orders and the Order has many OrderDetails, so f you are binding to a Customer, then which OrderID would you like to display, similarly to the OrderDetails. But When you are binding to the many-side of a many to one relation, then you only have one entity at the other side to bind to.

Anyway please use something like:

<asp:TemplateField HeaderText="Customer" SortExpression="ProductName">
                            <ItemTemplate>
                                <asp:Label ID="Product" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Order.Customer.CompanyName")%>' />
                            </ItemTemplate>
</asp:TemplateField>

Please note that you should supply a prefetchPath to the same dsitination to the DataSource. For the above example you should set the LLBLGenProDataSource.PrefetchPathToUse as follows:

            OrderDetailsDS.PrefetchPathToUse = new PrefetchPath((int)EntityType.OrderDetailEntity);
            OrderDetailsDS.PrefetchPathToUse.Add(OrderDetailEntity.PrefetchPathOrder).SubPath.Add(OrderEntity.PrefetchPathCustomer);
ntext
User
Posts: 8
Joined: 11-May-2009
# Posted on: 29-Jun-2009 11:00:36   

No. I give customers orders and orderdetails tables as an example. Let me ask my question this way.

I have three tables.

TABLE_A TABLE_B TABLE_C

TABLE_A has 1:1 relation with TABLE_B and TABLE_B has relationship with TABLE_C.

I m binding TABLE_A gridview by using this simple code; TABLE_ACollection col = new TABLE_ACollection(); and relations added to code.. and lastly calling col.GetMulti(somefilter, 0, null, relationToUse); GridView1.DataSource = col; GridView1.DataBind();

so I m not using any "DS"

on the code view of the aspx page binding TABLE_A field by using asp:boundfields as usual.

But I need to show a field from TABLE_C by using <template field> any other methods.

think TABLE_B as backbone the other two tables connects to.

just trying to reach TABLE_C field from TABLE_A collection. TABLE_C and TABLE_A has no relationship except one field from TABLE_B.

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 29-Jun-2009 14:15:12   

TABLE_A has 1:1 relation with TABLE_B and TABLE_B has relationship with TABLE_C.

Which kind of relation is the one between B & C?

I m binding TABLE_A gridview by using this simple code; TABLE_ACollection col = new TABLE_ACollection(); and relations added to code.. and lastly calling col.GetMulti(somefilter, 0, null, relationToUse); GridView1.DataSource = col; GridView1.DataBind();

so I m not using any "DS"

Wouldn't make a difference, use the aspx code I've posted above, and pass the prefetchPath to the GetMulti() method.

ntext
User
Posts: 8
Joined: 11-May-2009
# Posted on: 29-Jun-2009 15:11:34   

1:1 relation

Between TABLE_A and TABLE_B 1:1 relation Between TABLE_B and TABLE_C 1:1 relation

When using eval function TABLE_B.AnyField comes right away. Because TABLE_A entity = new TABLE_A(); entity.TABLE_B.Any field comes but entity.TABLE_B.TABLE_C comes as TABLE_C collection so I cant reach TABLE_C.AnyField.

Thanks for your effort.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 29-Jun-2009 15:24:24   

When using eval function TABLE_B.AnyField comes right away. Because TABLE_A entity = new TABLE_A(); entity.TABLE_B.Any field comes but entity.TABLE_B.TABLE_C comes as TABLE_C collection so I cant reach TABLE_C.AnyField.

So the relation between TABLE_B and TABLE_C is not 1:1 relation

ntext
User
Posts: 8
Joined: 11-May-2009
# Posted on: 29-Jun-2009 15:44:30   

Aha. Sorry. Really, one to one relationship forgotten to add rage

Thanks for your effort and understanding. Sorry to disturb you all.