Solved

# combine for loop

Posted on 2014-08-28
101 Views
Last Modified: 2014-08-28
I am trying to take two selected items and insert it into the database.
I need to loop through and if first is selected and second is selected insert together.

if only one first is selected and 3 items are selected for second. then  it will take the first selected item and insert 3 times with all 3 second items selected.

example

selected

first      Second
3           2
1
4
out put

3        2
3        1
3        4

or

Selected

first      second
3            2
4            3
1
out put
3            2
3            3
4            3
4            2
1            2
1            3

for (int j = 0; j < first.Items.Count; j++)
{
if (first.Items[j].Selected)
{
Insert(true,first.Items[j].Value, second.Items[j].Selected);
}
}
for (int j = 0; j < second.Items.Count; j++)
{
if (first.Items[j].Selected,second.Items[j].Selected)
{
Insert(true,first.Items[j].Value);
}
}
0
Question by:sevensnake77
4 Comments

LVL 55

Accepted Solution

You need a nested loop. Also, the lack of indent makes your code very difficult to read.
for (int j = 0; j < first.Items.Count; j++)
{
if (first.Items[j].Selected)
{
for (int j = 0; j < second.Items.Count; j++)
{
if (second.Items[j].Selected)
{
// DO YOUR INSERTION HERE, POSSIBLY:
Insert(true,first.Items[i].Value, second.Items[j].Selected);
}
}
}
}

0

LVL 9

Author Comment

can not have to j local variable exsits already.
0

LVL 9

Author Comment

It is not not working correctly it selects first and second if first is select but not the second in the same row of the list box then it fills it in itself.

example

first        second
3             4
2             8 << Not selected

but selects it anyway because 2 is selected in first.

0

LVL 9

Author Closing Comment

thanks, just int a instead of J for the other.
0

## Join & Write a Comment Already a member? Login.

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!