Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have a mysql query that inner joins two tables and i have griview on my asp page. Is there way to get a value from the inner joined table using <%# Eval %>. For example <%# Eval ("inner_joined_table_name.column_name")%>
Posted
Comments
Sandeep Mewara 5-Jul-12 5:54am    
Updated my answer - check out.

Yes!

Eval will look for the column in the query result. Just make sure that your query fetches the column after the join of tables.


UPDATE:
based on what you ask:
I do have a gridview with the following fields:
<asp:Label ID="Desc" runat="server" Text='<%# Eval("description") %>'/> 
<asp:Label runat="server" ID="Type" Text='<%# Eval("description")%>'/>
The first field is fine and it gets the description from the first table, the second field needs to get the description from the inner joined table. My query looks like this:
        MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("SELECT rank_distribution.rank_distribution_id,rank_distribution.description ,rank_distribution.amount,transaction_distribution.description FROM rank_distribution INNER JOIN transaction_distribution ON transaction_distribution.transaction_distribution_id = rank_distribution.transaction_distribution_id WHERE rank_distribution.rank_transaction_type_id = @rank_transaction_type_id", con);
        cmd.Parameters.AddWithValue("@rank_transaction_type_id", Request.QueryString["rank_transaction_type_id"]);


All you need to do is to have alias of the column names such that there are two description columns clearly identifiable at the end.

Try:
HTML
<asp:Label ID="Desc" runat="server" Text='<%# Eval("RD_description") %>'/> 
<asp:Label runat="server" ID="Type" Text='<%# Eval("TD_description")%>'/>

Query:
SQL
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("SELECT rank_distribution.rank_distribution_id,rank_distribution.description as RD_description,rank_distribution.amount,transaction_distribution.description as TD_description FROM rank_distribution INNER JOIN transaction_distribution ON transaction_distribution.transaction_distribution_id = rank_distribution.transaction_distribution_id WHERE rank_distribution.rank_transaction_type_id = @rank_transaction_type_id", con);
 
Share this answer
 
v2
Comments
Abhinav S 5-Jul-12 4:27am    
This is what it looks like (based on the documentation). My 5.
Sandeep Mewara 5-Jul-12 5:55am    
Thanks Abhinav.
Ruwaldo 5-Jul-12 6:03am    
Thanks for the response, but i did try this already and i got this same error
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'RD_description'.
Sandeep Mewara 5-Jul-12 6:15am    
Run the raw query (directly in database tool query it - not via code) and see what result you get. There should be two columns for each description. You need to have separate name of the field to bind in grid.
Ruwaldo 5-Jul-12 6:30am    
Yes if the columns name are different it works...
Eval (from documentation[^]) -
"The navigation path from the container object to the public property value to be placed in the bound control property. This must be a string of property or field names separated by periods, such as Tables[0].DefaultView.[0].Price in C# or Tables(0).DefaultView.(0).Price in Visual Basic."

As far as I understand, you cannot directly bind to a table.
However, you could use a datatable or gridview column and bind to it.
 
Share this answer
 
v3
Comments
Ruwaldo 5-Jul-12 4:43am    
I do have a gridview with the following fields:
<asp:Label ID="Desc" runat="server" Text='<%# Eval("description") %>'/>
<asp:Label runat="server" ID="Type" Text='<%# Eval("description")%>'/>
The first field is fine and it gets the description from the first table, the second field needs to get the description from the inner joined table. My query looks like this:
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("SELECT rank_distribution.rank_distribution_id,rank_distribution.description ,rank_distribution.amount,transaction_distribution.description FROM rank_distribution INNER JOIN transaction_distribution ON transaction_distribution.transaction_distribution_id = rank_distribution.transaction_distribution_id WHERE rank_distribution.rank_transaction_type_id = @rank_transaction_type_id", con);
cmd.Parameters.AddWithValue("@rank_transaction_type_id", Request.QueryString["rank_transaction_type_id"]);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900