combine for loop

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);
}
}

Open in new window

LVL 9
Seven priceFull StackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
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);
                       }
               }
        }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Seven priceFull StackAuthor Commented:
can not have to j local variable exsits already.
0
Seven priceFull StackAuthor Commented:
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.

Open in new window

0
Seven priceFull StackAuthor Commented:
thanks, just int a instead of J for the other.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.