You may populate the DropdownList setting its DatafieldValue/DataTextField as"city" and binding it to the same datasource. Then remove the duplicates using code..
You may populate the DropdownList setting its DatafieldValue/DataTextField as"city" and binding it to the same datasource. Then remove the duplicates using code..
moquamar
Member
46 Points
149 Posts
Distinct Values in DropdownList
Apr 09, 2012 05:38 PM|LINK
I have the following code which returns all City names from the database. The column City in the database contains duplicate names.
In my DropdownList; I want to have only distinct City names; how do i do it?
<asp:DropDownList runat="server" id="lstCity" AppendDataBoundItems="True"
DataSourceID="SqlDataSource1"
DataTextField="City" DataValueField="City"
onselectedindexchanged="lstCity_SelectedIndexChanged"
AutoPostBack="True" >
budugu
All-Star
41108 Points
6019 Posts
Re: Distinct Values in DropdownList
Apr 09, 2012 05:49 PM|LINK
Why don't you filter duplicate values by using DISTINCT key word at DB level
"Don't be afraid to be wrong; otherwise you'll never be right."
tdmca
Contributor
2396 Points
661 Posts
Re: Distinct Values in DropdownList
Apr 09, 2012 05:54 PM|LINK
again configure the SqlDataSource
use query in SqlDataSource
select distinct city from table_name
shatru
Participant
1513 Points
292 Posts
Re: Distinct Values in DropdownList
Apr 09, 2012 05:59 PM|LINK
You Select statement should be somthing like:
Regards
Ajatshatru
Dragons Lab
moquamar
Member
46 Points
149 Posts
Re: Distinct Values in DropdownList
Apr 09, 2012 06:55 PM|LINK
I cannot do it within the SQL Statment since i am returning the entire table (including duplicate City names) for display purposes.
I need to do it somehow other than using the select statement.
tdmca
Contributor
2396 Points
661 Posts
Re: Distinct Values in DropdownList
Apr 09, 2012 07:06 PM|LINK
when you create sqldatasource you have option to specify sql query
otherwise leave sql datasource use sqldataadapter and dataset
SqlDataAdapter da=new SqlDataAdapter("select distinct city from table",con);
DataSet ds=new DataSet();
da.Fill(ds);
dpcity.DataSource=ds;
dpcity.DataTextField="city";
dpcity.DataBind();
moquamar
Member
46 Points
149 Posts
Re: Distinct Values in DropdownList
Apr 09, 2012 07:13 PM|LINK
1. I cannot modify my datasource due to sonce other restrictions in the app.
2. I must use the same datasource that is displaying the rest of the data within the page.
3. I must modify the asp code that I provided earlier or do something in the code behid to filter dropdown.
basheerkal
Star
10672 Points
2426 Posts
Re: Distinct Values in DropdownList
Apr 09, 2012 07:22 PM|LINK
You may populate the DropdownList setting its DatafieldValue/DataTextField as"city" and binding it to the same datasource. Then remove the duplicates using code..
Please refer this link to see how it can be done.
http://dotnetguts.blogspot.in/2006/10/removing-duplicates-item-from.html
(Talk less..Work more)
moquamar
Member
46 Points
149 Posts
Re: Distinct Values in DropdownList
Apr 09, 2012 10:47 PM|LINK
That is exactly what I needed. thanks