Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sort Bound Drop Drop List

Posted on 2010-11-24
5
223 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 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

809 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