Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
am adding the excelsheet names to dropdownlist
problem is if the existing file is uploaded for overwrite then the dropdownlist values are added twice ,how can i validate that the item with same name must be added only once
Posted

As a simple solution, write your own method to eliminate the duplicate entry as:

C#
public static void RemoveDuplicateItems(DropDownList ddl)
    {
        for (int i = 0; i < ddl.Items.Count; i++)
        {
            ddl.SelectedIndex = i;
            string currValue = ddl.SelectedItem.ToString();
            for (int counter = i + 1; counter < ddl.Items.Count; counter++)
            {
                ddl.SelectedIndex = counter;
                if (currValue == ddl.SelectedItem.ToString())
                {
                    ddl.Items.RemoveAt(counter);
                    counter = counter - 1;
                }
            }
        }
 
Share this answer
 
Before Adding Items to DropDown Lists check Existing Item using Contains Property
C#
<pre lang="cs">DropDownList ddl= new DropDownList();


      if(!ddl.Items.Contains(new ListItem("item")))
      {
          ddl.Items.Add(new ListItem("item"));


      }



If you are calling DataBind() method make sure the List is Clear First

C#
if(ddl.Items.Count>0)
ddl.Items.Clear();
 
Share this answer
 
v2

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