Danny Vo
Danny Vo

Reputation: 17

Adding Value to databound DropDownlist (asp.net)

I am creating a website which has 1 dropdownlist that bound to Northwind database, Customers table. This Drop downlist will list all the countries from the tables. I have tried to add "Japan" (which was not in the list) to this dropdownlist. It was added but it always appear at the top (or default value). My question is: Is it possible to add Japan and not make it appear at the top, so it will follow the alphabetical order?

This is my code:

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" 
            AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Country" 
            DataValueField="Country"><asp:ListItem Text ="Japan" />
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>" 

            SelectCommand="SELECT Country FROM Customers GROUP BY Country ORDER BY Country">
        </asp:SqlDataSource>

Upvotes: 1

Views: 954

Answers (1)

Thit Lwin Oo
Thit Lwin Oo

Reputation: 3438

You can try this way in your sql script.

SELECT Country
FROM
(
    SELECT Country 
    FROM Customers 
    GROUP BY Country 

    UNION ALL

    SELECT 'JAPAN'
) M
ORDER BY Country

Upvotes: 2

Related Questions