Link to home
Start Free TrialLog in
Avatar of synapse88
synapse88

asked on

How To Sort Drop Down List - ASP.NET 2.0, VB.NET

Please check the code below and tell me how to modify so the county drop list will be sorted by a-z.

Currently the list is being ordered by the identity field which isn't even remotely alphabetical.  The table which has the data for the drop down list just has two columns, the identity field (an int), and a "name" field which is what i'm trying to sort the list by.

Thanks for any ideas/info.  There is no mention of the drop down's ID in the VB code behind page, but I can add some code there if that is the best way to accomplish this.
<!--Data Source for the entire form-->
<asp:SqlDataSource ID="sqlLocation" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>"
SelectCommand="SELECT * FROM Locations">
</asp:SqlDataSource>
 
<!--Data Source for the County Drop Down-->
<asp:SqlDataSource ID="sqlCounties" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>"
SelectCommand="SELECT * FROM Counties ORDER BY name">
</asp:SqlDataSource>
 
.
.
.
 
 
<!-- Actual County Drop Down-->
<asp:DropDownList ID="cboCounty" runat="server" CssClass="TextBox" AppendDataBoundItems="true"
DataSourceID="sqlCounties" DataTextField="name" DataValueField="identity"
SelectedValue='<%# Bind("county") %>' >
<asp:ListItem Value="">Not Entered</asp:ListItem>
</asp:DropDownList>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of synapse88
synapse88

ASKER

But the only problem is it is not.  When I access the drop down in the application, the drop down is not sorted, and I can tell it's still sorted the identity field. :(
Is there any place in your code-behind that modifies the SelectCommand before you bind the drop down list?
Also, what if you take out this line SelectedValue='<%# Bind("county") %>'?
Avatar of Christopher Kile
synapse88,

This is a long shot, but have you tried it without specifying the CssClass?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another try. Change your SelectCommand to this:
SelectCommand="SELECT [name], identity FROM Counties ORDER BY [name]
"name" is a keyword in SQL, so try to use bracket around it.
I got it figured out, it was just a simple error I made when testing my updates...unrelated to the actual code.

I'll split the points since you were both quite helpful.