Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5460
  • Last Modified:

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

0
synapse88
Asked:
synapse88
  • 3
  • 2
  • 2
2 Solutions
 
prairiedogCommented:
I don't see any problem with the code...the drop down list should be sorted by name because of this line of code:
SelectCommand="SELECT * FROM Counties ORDER BY name">
0
 
synapse88Author Commented:
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. :(
0
 
prairiedogCommented:
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") %>'?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Christopher KileCommented:
synapse88,

This is a long shot, but have you tried it without specifying the CssClass?
0
 
Christopher KileCommented:
Also, have you defined this DropDownList within the context of a FormView or some other container that has a field called "county"?  I don't know what effect this may have, but as I said before, I'm looking at long shots.
0
 
prairiedogCommented:
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.
0
 
synapse88Author Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now