Solved

Sort Bound Drop Drop List

Posted on 2010-11-24
5
214 Views
Last Modified: 2012-05-10
I have a drop down list that is bound to a SQL table. I'd like for the list to appear as being sorted in order to make it easier for the user.

In the following statement:

sComm = New Data.SqlClient.SqlCommand("SELECT Contacts From Table where DeptName = '" & DropDownList1.SelectedItem.Text & "'", sConn)

I changed it to:

sComm = New Data.SqlClient.SqlCommand("SELECT Contacts From Table ORDER BY DeptName where DeptName = '" & DropDownList1.SelectedItem.Text & "'", sConn)

But it's had no effect.
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

        If DropDownList1.SelectedIndex = 0 Then

            TextBox1.Text = ""

        Else



            intcount = 0

            Dim sConn As Data.SqlClient.SqlConnection

            Dim sComm As Data.SqlClient.SqlCommand

            Dim sAdapt As Data.SqlClient.SqlDataAdapter





            sConn = New Data.SqlClient.SqlConnection("server=SQLServer;database=Datatbase;UID=UserID;PWD=Password")

            sComm = New Data.SqlClient.SqlCommand("SELECT Contacts From Table ORDER BY DeptName where DeptName = '" & DropDownList1.SelectedItem.Text & "'", sConn)

            sAdapt = New Data.SqlClient.SqlDataAdapter(sComm)

            sConn.Open()



            Dim r As Data.SqlClient.SqlDataReader = sComm.ExecuteReader()

            While r.Read()

                Dim strEmail As String = CStr(r("Contacts"))

                TextBox5.Text = strEmail

            End While

            r.Close()

Open in new window

0
Comment
Question by:JB4375
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
Comment Utility
The SqldataAdapter that have the dropdown is sorted??? Because On this line you only sort the list to search for a text when  a department is selected but you must Modify the SqlDataadapter query that is binding with the dropdown
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
Comment Utility
Check if the select query that have the SqlDataAdapter that is binding to the Dropdownlist is sorted by DeptName (Maybe you query must be like this if only shows the departments on the Dropdown :
SalDataAdapter select query ->  SELECT IdDpt,DeptName from Departaments_table ORDER BY DeptName) an this will show your Dropdown list order by Dept.
0
 
LVL 1

Author Comment

by:JB4375
Comment Utility
K-Des,

Thanks for the quick response, but this flew completely over my head. Are you referring to this line:

sAdapt = New Data.SqlClient.SqlDataAdapter(sComm)

that needs to be edited or ???
0
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
Comment Utility
Nope my friend, If you have a Dropdownlist control that is binding to the SqlDatadapter1 then. on design time you must chek the query of this sqlDataAdapter and add the order by clausule to this query and then your dropdown list will be sorted. I illustrated to you by images down here

 1 Step2.png
3.png
0
 
LVL 1

Author Closing Comment

by:JB4375
Comment Utility
Thanks for the assist!!!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

728 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

10 Experts available now in Live!

Get 1:1 Help Now