Search code examples
c#asp.netsql-serverwebformsdropdown

How to add various SQL columns in the same dropdownlist ASPX


For example, the database has these columns:

Name   Type   Place

I am already showing all the names in a dropdownlist; how can I show all three columns - Name, Type, Place - in that same dropdown?

This is what I got

<tr>
    <td align="left" valign="top">Name:</td>
    <td align="left" colspan="3" style="height: 21px" valign="top">
    <asp:DropDownList ID="DropD_Name" runat="server" 
         BackColor="White" AppendDataBoundItems="true" 
         DataSourceID="SqlDS_Name" DataTextField="Name" DataValueField="Name" 
         ForeColor="Black" 
         SelectedValue='<%# Bind("Name") %>' Width="290px">
        <Items>
            <asp:ListItem Selected="True"></asp:ListItem>
        </Items>
    </asp:DropDownList>
    </td>
</tr>

SqlDS_Name:

<asp:SqlDataSource ID="SqlDS_Name" runat="server" 
     ConnectionString="<%$ ConnectionStrings:BlablablaConnectionString %>"
     SelectCommand="SELECT Name, Type, Place FROM dbo.vwnames">
</asp:SqlDataSource>

Thanks for your help finally this is the solution for the query

SELECT CONCAT (Name,' - ',Type,' - ',Place) AS TextColumn,Name AS NameInfo FROM dbo.vwnames

Solution

  • SELECT Name, Type, Place FROM dbo.vwnames
    

    Change to :

    SELECT Name + ',' + Type + ',' + Place AS TextColumn,Name FROM dbo.vwnames
    

    result :

    <asp:SqlDataSource ID="SqlDS_Name" runat="server" 
         ConnectionString="<%$ ConnectionStrings:BlablablaConnectionString %>"
         SelectCommand="SELECT Name + ',' + Type + ',' + Place AS TextColumn,Name FROM dbo.vwnames">
    </asp:SqlDataSource>
    

    And DropDownList change to :

    <asp:DropDownList 
       ID="DropD_Name"
       runat="server" 
       BackColor="White" 
       AppendDataBoundItems="true"
       DataSourceID="SqlDS_Name" 
       DataTextField="TextColumn" 
       DataValueField="Name" 
       ForeColor="Black" 
       SelectedValue='<%# Bind("Name") %>' 
       Width="290px">
      <Items>
        <asp:ListItem Selected="True"></asp:ListItem>
      </Items>
    </asp:DropDownList>