vendredi 27 avril 2018

How can I use the result from a SQL Data Source query as text using the parameter that was passed to the URL?

I am have a page that returns a list of assets from a SQL table belonging to a user using based on their UserID which is already passed via the URL (e.g. UserDetails.aspx?UserID=1) and there is a SQL Data Source that uses that to pull the information:

asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:AssetManagementConnectionString %>" SelectCommand="SELECT * FROM [Assets_Belonging_To_Users] WHERE ([UserID] = @UserID)">
            <SelectParameters>
                <asp:QueryStringParameter DefaultValue="0" Name="UserID" QueryStringField="UserID" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>

That part works fine, however, I am also wanting to use the UserID in the query of my second data source to then use the result of that query as a string of text.

This is the second Data Source:

            <asp:SqlDataSource ID="SqlDataSourceName" runat="server" ConnectionString="<%$ ConnectionStrings:AssetManagementConnectionString %>" SelectCommand="SELECT [First_Name], [Last_Name] FROM [tblUsers] WHERE ([UserID] = @UserID)">
                <SelectParameters>
                    <asp:QueryStringParameter DefaultValue="0" Name="UserID" QueryStringField="UserID" Type="Int32" />
                </SelectParameters>
        </asp:SqlDataSource>

So I am wanting to return First_Name and Last_Name as such:

Assets Belonging to [First_Name] +' '+ [Last_Name] (as specified in the select query)

Having researched I believe that the only way to do this would be an asp:Label which I have tried and couldn't quite get working:

<asp:Label ID="lblName1" runat="server" Text="<%= Request.ToString("UserID") %>" 

I feel as if I need to, somehow, specify which Data Source to use (SqlDataSourceName). I apologise if this is very far off of where I need to be or if there isn't enough information in the question.

Thank you.

P.S. I am aware that I could include the columns on the view and use those however it will show the First_Name + Last_Name for every record.




Aucun commentaire:

Enregistrer un commentaire