Click here to Skip to main content
15,174,684 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I want to add current year to drop down list dynamically. But if the value is already present in the drop down list then it should not be added again. I am able to add year to drop down list. But it gets added again and again every time the page is loaded. How to avoid this?

For example : Current year is 2012. Now the drop down list should only contain 2012. And when 2013 will come the drop down list should contain 2012 as well as 2013 and so on.

Thanks in advance...
StackQ 15-Dec-12 1:51am
so simple,for each value u r inserting check them,in dropdownlist,if exist not add,else add,Follow logic

1 solution

If I understood correctly, you want to remove duplicate items from DropDown.. So here is your solution : (source)

public static void RemoveDuplicateItems(DropDownList ddl)

for (int i = 0; i < ddl.Items.Count; i++)
ddl.SelectedIndex = i;
string str = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
ddl.SelectedIndex = counter;
string compareStr = ddl.SelectedItem.ToString();
if (str == compareStr)
counter = counter - 1;
Menon Santosh 15-Dec-12 4:03am
Good work my +5
[no name] 15-Dec-12 4:05am
Ty mate :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900