Solved

2 Foreach loops

Posted on 2011-09-20
3
139 Views
Last Modified: 2012-05-12
I have 2 rows of data like below and my code. I get the rows from database, I check for comma, and add to listbox. But what I'm doing below actually
add 5 rows and I think it's because of the 2 for each loops nested. I need 3 rows...not 5. Two of the rows get repeated...


     Bob Jones, Dave Jones      1, 11
        Jane Doe            12


I should get:
Bob Jones
Dave Jones
Jane Doe

But I get:

Bob Jones
Dave Jones
Bob Jones
Dave Jones
Jane Doe
foreach (var p in results)
                {
                    if (p.HCproviders.Contains(","))
                    {
                        String[] providerValues = p.HCproviders.Split(new Char[] { ',' });
                        String[] idValues = p.hcids.ToString().Split(new Char[] { ',' });
                        foreach (String i in providerValues)
                        {
                            foreach(string j in idValues)
 
                              lb.Items.Add(new ListItem(i, j));
                        } 
 

                    }
                    else
                    lb.Items.Add(new ListItem(p.HCproviders, p.hcids.ToString()));

                }

Open in new window

0
Comment
Question by:Camillia
3 Comments
 
LVL 16

Accepted Solution

by:
disrupt earned 500 total points
Comment Utility
source:
http://forum.logicalgamers.com/source-codes/31626-c-remove-duplicates-listbox.html
public void RemoveListboxDuplicates(ListBox ListBox) 
  { 
    List<String> Removed = new List<String>(); 
    for (int I = 0; I < ListBox.Items.Count;I++) 
    { 
    if (!Removed.Contains(ListBox.Items[I].ToString())) 
    { 
    Removed.Add(ListBox.Items[I].ToString()); 
    } 
    } 
    ListBox.Items.Clear(); 
    foreach (String S in Removed) 
    { 
    ListBox.Items.Add(S); 
    } 

  }

Open in new window

0
 
LVL 5

Expert Comment

by:zvytas
Comment Utility
Use the following code instead:

foreach (var p in results)
                {
                    if (p.HCproviders.Contains(","))
                    {
                        String[] providerValues = p.HCproviders.Split(new Char[] { ',' });
                        String[] idValues = p.hcids.ToString().Split(new Char[] { ',' });
                        int index = 0;
                        foreach (String i in providerValues)
                        {
                            lb.Items.Add(new ListItem(i, idValues[j]));
                        }
 

                    }
                    else
                    lb.Items.Add(new ListItem(p.HCproviders, p.hcids.ToString()));

                }
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
You have
 lb.Items.Add(new ListItem(i, idValues[j]));

What is j? did you mean "index"??
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now