Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop method from calling themselves recursively?
    primarykey
    data
    text
    <p>I have two tables, <code>employees</code> and <code>project</code>, in <code>listbox2</code>, I show all the <code>employees</code> and in <code>listbox1</code> all the <code>projects</code>, now obviously one employee can be involved in many projects and one project could have many employee. So I have this <code>EmployeeProject</code> that maps the many to many relation that exists. What I want is, if user click a project name in first listbox, then all employees in that project should be selected in listbox2. Also, when a user clicks a item in listbox2, (an employee) all project of which that employee is a part should be selected in listbox1</p> <p><strong>But</strong> If I use ListBox.SelectedIndexChanged event for this process, and select even a single value in listbox2 then it would trigger the SelectedIndexChagned for listbox2, and that would start working by selecting all items in listbox1 that current employee is a part of, but again, as soon as even one item in listbox1 is selected, it would fire up its SelectedIndexChanged event, and it would go on forever like this. So what's the solution of this? So far, I've done this..</p> <pre><code> private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { // Load the list of employees cmd.CommandText = "SELECT EmpName FROM Employee WHERE EmpID IN(SELECT EmpID FROM EmployeeProject WHERE ProjectID =(SELECT ProjectID FROM Project WHERE ProjectName = '" + listBox1.SelectedItem.ToString() + "')) ORDER BY EmpId"; var rdr = cmd.ExecuteReader(); listBox2.Items.Clear(); // right now, I am doing this to escape this recursive loop, but thats not what I want while (rdr.Read()) { listBox2.Items.Add(rdr.GetString(0)); } rdr.Close(); this.AutoScroll = true; } private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { // Load the list of projects cmd.CommandText = "SELECT ProjectName FROM Projects WHERE ProjectID IN(SELECT ProjectID FROM EmployeeProject WHERE EmpId=(SELECT EmpId FROM Employee WHERE EmpName= '" + listBox2.SelectedItem.ToString() + "')) ORDER BY ProjectID"; var rdr = cmd.ExecuteReader(); listBox1.Items.Clear(); // again, I don't want to clear, but select all those employee in listbox1 that are involved in this selected project, but can't do it because it would cause infinite recursion of these while (rdr.Read()) { listBox2.Items.Add(rdr.GetString(0)); } rdr.Close(); this.AutoScroll = true; } </code></pre> <p>So? What should I do to achieve what I want to achieve? And how would I avoid that recursion? I know this way also works what I just showed, but I don't want to clear up and show up again, (this might confuse a simple user). I want for each selection, values corresponding to that selection be selected in other listbox (without causing recursion of course!). How do I do it?</p> <p><em>EDIT</em> I don't know how can I select multiple items in listbox programmatically, so if you could tell that, it would be great!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload