I am trying to sort my data (which is in a datalist) by its price. Meaning that there is DropDownList for user to choose if they want the price from the most expensive to the cheapest and vice versa. The prices stored in my database is in random sequence according to my catID. I have written the code as follows but it did not sort accordingly to what i wrote. What have i done wrong here? Please advice me.
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
bindDropDownList();
}
private void bindDropDownList()
{
DropDownList1.DataTextField = "price";
DataList1.DataSourceID = null;
DataList1.DataSource = getReader();
DropDownList1.DataBind();
}
private SqlDataReader getReader()
{
SqlDataReader reader = null;
if(DropDownList1.Text == "-Select-")
{
string strConnectionString =
ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText ="SELECT * FROM [Category ] WHERE catID<= 20";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
else if (DropDownList1.SelectedValue == "Price - Highest to Lowest")
{
string strConnectionString =
ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "SELECT catID, packageName, price, description1, description2, image1, image2 FROM Category WHERE catID <= 20 ORDER BY price desc";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
else if (DropDownList1.DataTextField == "Price - Lowest to Highest")
{
/string strConnectionString =
ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "SELECT catID, packageName, price, description1, description2, image1, image2 FROM Category WHERE catID <= 20 ORDER BY price";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
return reader;
}
My .aspx code:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" AppendDataBoundItems="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" Height="18px"
Width="184px">
<asp:ListItem>-Select-</asp:ListItem>
<asp:ListItem>Price - Highest to Lowest</asp:ListItem>
<asp:ListItem>Price - Lowest to Highest</asp:ListItem>
</asp:DropDownList>
I think this line is wrong, might never evaluates to true
should be
or even better
Update:
The
AppendDataBoundItems="true"could be messing with your dropdown items. The results of Lowest and Highest are being merged may be that’s why you couldn’t see the difference.Remove the default list items and put them in your code-behind
For alternative approach, check DropDownList AppendDataBoundItems (first item to be blank and no duplicates)