Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 109
  • Last Modified:

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

0
Seven price
Asked:
Seven price
  • 3
1 Solution
 
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
 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now