Solved

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

Posted on 2008-06-20
7
5,207 Views
Last Modified: 2008-06-20
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
Comment
Question by:synapse88
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Accepted Solution

by:
prairiedog earned 250 total points
ID: 21834758
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
 

Author Comment

by:synapse88
ID: 21834773
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
 
LVL 22

Expert Comment

by:prairiedog
ID: 21834808
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:Christopher Kile
ID: 21834849
synapse88,

This is a long shot, but have you tried it without specifying the CssClass?
0
 
LVL 23

Assisted Solution

by:Christopher Kile
Christopher Kile earned 250 total points
ID: 21834863
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
 
LVL 22

Expert Comment

by:prairiedog
ID: 21834878
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
 

Author Comment

by:synapse88
ID: 21834914
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now