I have a dropdownlist with duplicate values that I want to eliminate. The datasource for the list is a complex stored procedure. In the stored procedure I use select distinct. When I run the procedure and enter the parameter values from within the server
explorer the list does not contain duplicates. When I select the dropdown from the webform it contains the duplicates. Any suggestions on how to resolve this? I would really appreciate some help as I have spent a number of hours trying to resolve it.
Hi, can anyone help to correct the below codes, below are the DropDownList, SQL dataSource and GridView. What I want was when selected the item in the dropDownList, the gridView will displays the selected data. But somehow, some error occurred and can't
display the data. Can anyonehHelp?
It's really not a good idea to post unrelated questions in old threads. If you have a new question, start a new thread and then more people will see your problem and try to help.
Also, "some error occurred" is not nearly as helpful as "I saw this error message ______ and it pointed to this line _____ in my code".
Cheers,
Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com Do the impossible, and go home early.
Bidler
Member
15 Points
5 Posts
Dropdown list with duplicates
May 24, 2006 02:05 AM|LINK
I have a dropdownlist with duplicate values that I want to eliminate. The datasource for the list is a complex stored procedure. In the stored procedure I use select distinct. When I run the procedure and enter the parameter values from within the server explorer the list does not contain duplicates. When I select the dropdown from the webform it contains the duplicates. Any suggestions on how to resolve this? I would really appreciate some help as I have spent a number of hours trying to resolve it.
wannesh
Member
610 Points
132 Posts
Re: Dropdown list with duplicates
May 24, 2006 02:48 PM|LINK
if you populate your DDL in your sub page_load
try putting your code between
If not Page.IsPostBack Then
.....
end if
Bidler
Member
15 Points
5 Posts
Re: Dropdown list with duplicates
May 26, 2006 02:01 AM|LINK
Thanks for the reply wannesh, but the code is already in the
if not Page.IsPostBack
...
endif
KD34XBR960
Member
10 Points
2 Posts
Re: Dropdown list with duplicates
Sep 14, 2006 03:22 PM|LINK
I needed to remove duplicate years from a DropDownList and so I created the following class in C#:
public class Ddl
{
public Ddl()
{
}
public static void RemoveDuplicateItems(DropDownList ddl)
{
for (int i = 0; i < ddl.Items.Count; i++)
{
ddl.SelectedIndex = i;
string year = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
{
ddl.SelectedIndex = counter;
string compareYear = ddl.SelectedItem.ToString();
if (year == compareYear)
{
ddl.Items.RemoveAt(counter);
counter = counter - 1;
}
}
}
}
}
After I created the class, I placed the file in my project's App_Code folder. I then used the following code to call the method:
Ddl.RemoveDuplicateItems(ddlYear);
In this example, ddlYear is the name of the dropdown.
c# DropDownList
Ganesh@Nilgris
Contributor
6074 Points
2354 Posts
Re: Dropdown list with duplicates
Sep 15, 2006 04:19 AM|LINK
A better solution
select the distinct years from the database and display it in the dropdownlist
Please Mark As Answer If my reply helped you.
edwardwong
Member
50 Points
10 Posts
Re: Dropdown list with duplicates
Sep 26, 2006 03:43 AM|LINK
Hi, can anyone help to correct the below codes, below are the DropDownList, SQL dataSource and GridView. What I want was when selected the item in the dropDownList, the gridView will displays the selected data. But somehow, some error occurred and can't display the data. Can anyonehHelp?
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script runat="server">
Sub Get_Book_Type (Src As Object, Args As EventArgs)
Dim SQLString As String
SQLString = "SELECT BookID, BookTitle, BookPrice, BookQty FROM Books " & _
"WHERE BookType = '" & TypeList.SelectedValue & "' " & _
"ORDER BY BookID"
BookSource2.SelectCommand = SQLString
End Sub
</SCRIPT>
</head>
<body>
<form id="Form1" Runat="Server">
<h3>View Book Types</h3>
<asp:SqlDataSource ID="BookSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:wongConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:wongConnectionString1.ProviderName %>"
SelectCommand="SELECT [BookID], [BookTitle], [BookPrice], [BookQty], [BookType] FROM [Books]">
</asp:SqlDataSource>
<asp:DropDownList id="TypeList" Runat="Server"
DataSourceID="BookSource1"
DataTextField="BookType"
DataValueField="BookType"
AutoPostBack="True"
OnSelectedIndexChanged="Get_Book_Type"/><br/><br/>
<asp:SqlDataSource ID="BookSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:wongConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:wongConnectionString1.ProviderName %>"
SelectCommand="SELECT [BookID], [BookTitle], [BookPrice], [BookQty]FROM [Books]WHERE [BookType]=@BookType">
</asp:SqlDataSource>
<asp:GridView id="BookGrid" DataSourceID="BookSource2" Runat="Server"/>
<br />
</form>
</body>
</html>
PeterBrunone
All-Star
18485 Points
3683 Posts
MVP
Re: Dropdown list with duplicates
Sep 26, 2006 03:34 PM|LINK
It's really not a good idea to post unrelated questions in old threads. If you have a new question, start a new thread and then more people will see your problem and try to help.
Also, "some error occurred" is not nearly as helpful as "I saw this error message ______ and it pointed to this line _____ in my code".
Cheers,
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.