Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a value already exist in list box before adding to it in asp.net

how can I check if the value is already present in a list-box, so that I can avoid duplicates?

I've added some values to the server-side list-box already, and when I add to list I get more duplicates.

How do I avoid duplicates?

lst_Viewers.Items.Add(reader["Name"].ToString());
like image 360
Murthy Avatar asked Jan 18 '23 11:01

Murthy


1 Answers

ListItem item = new ListItem(reader["Name"].ToString());
if ( ! lst_Viewers.Items.Contains(item) ){
    lst_Viewers.Items.Add(item);
}

or

var name = reader["Name"].ToString();
ListItem item = lst_Viewers.Items.FindByText(name);
if ( item  == null ){
    lst_Viewers.Items.Add(new ListItem(name));
}
like image 140
Tim Schmelter Avatar answered Feb 03 '23 23:02

Tim Schmelter