Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sort Bound Drop Drop List

Posted on 2010-11-24
5
Medium Priority
?
229 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
ID: 34206717
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
ID: 34206787
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
ID: 34206967
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 2000 total points
ID: 34207053
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
ID: 34207719
Thanks for the assist!!!
0

Featured Post

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.

Question has a verified solution.

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

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

772 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