0

I have in my Web a GridView that use SqlDatasource to insert data into the rows of the gridview.

the grid have for example: id name genre_id

now i have a table with songs and table with genres(genre_id,genre_name), and i want that in the grid will use genres_name and not the genres_id

This is the SqlDataSource for the GridView:

<asp:SqlDataSource ID="StationsDataSource" runat="server" 
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:TvSQLManagerConnectionString %>" 
        SelectCommand="SELECT [id],[name],[genre] FROM songs ORDER BY [name]" 
</asp:SqlDataSource>

1 Answer 1

1

Use SQL Join to get data from Genre table as well.

Replace your Query in SelectCommand like this

SELECT S.ID,S.NAME.G.GENRE_NAME AS GENRE FROM SONGS S
INNER JOIN 
GENERES G ON S.GENRE=G.GENRE_ID
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that i define in the gridview genre as the DataField of his colmun,and then he except to get genre but he get G.GENRE_NAME and he give me an error when i run it
@MTA : Either change the name in datagrid or change the query to have an alias ( i updated my answer for this)

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.