Click here to Skip to main content
15,921,174 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to sort my packages according to its price from the most expensive to the cheapest using DropDownList by getting all values from the database. And by default, users can already see all the packages that i have inserted into the database. However, i still get the same output as the default seen by the user whereby it is not sorted out accordingly to the price when i select the dropdownlist. What have i done wrong here?

This is my .aspx file:

C#
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" AppendDataBoundItems="true" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem>Choose one</asp:ListItem>
                <asp:ListItem>Price - high to low</asp:ListItem>
                <asp:ListItem>Price - low to high</asp:ListItem>
            </asp:DropDownList>


This is my .cs file:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            bindDropDownList();
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    
    }

    private void bindDropDownList()
    {

        DropDownList1.DataSource = getReader();
        DropDownList1.DataBind();

    }

    private SqlDataReader getReader()
    {
        
        string strConnectionString =
            ConfigurationManager.ConnectionStrings["ToysConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT package, price, description, image1, image2 FROM catalogue WHERE catalogueID <= 10 ORDER BY price desc";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        myConnect.Open();
        
        SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return reader;
    }
Posted
Updated 21-Jul-12 8:42am
v3

1 solution

change you code from
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }



to

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList();
    }
 
Share this answer
 
Comments
kellycx 22-Jul-12 0:44am    
I have added the code but the outcome is still the same. Is it that the price i save in database must be purely numbers? As i have some which is written as "$49.90 for a set of two".

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