Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8115201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:23:22+00:00 2026-06-06T03:23:22+00:00

I have two tables, employees and project , in listbox2 , I show all

  • 0

I have two tables, employees and project, in listbox2, I show all the employees and in listbox1 all the projects, now obviously one employee can be involved in many projects and one project could have many employee. So I have this EmployeeProject 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

But 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..

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

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?

EDIT I don’t know how can I select multiple items in listbox programmatically, so if you could tell that, it would be great!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:23:25+00:00Added an answer on June 6, 2026 at 3:23 am

    When you change the selected items of one listbox, you could temporarily disable the events on the other. For example:

    // Store the event handlers in private member variables.
    private System.EventHandler selectedEmployeeChanged = new System.EventHandler(this.lbEmployees_SelectedIndexChanged); 
    
    private void lbProjects_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            // Remove your event so that updating lbEmployees doesn't cause
            // lbEmployees_SelectedIndexChanged to get fired.
            lbEmployees.SelectedIndexChanged -= selectedEmployeeChanged;
    
            // other event handler logic
        }
        finally
        {
            // Ensure that the handler on lbEmployees is re-added,
            // even if an exception was encountered.
            lbEmployees.SelectedIndexChanged += selectedEmployeeChanged;
        }
    }
    

    Note: I have renamed your listBoxes (and the associated events) to be more readable. As per your description, listBox1 is now lbEmployees and listBox2 is now lbProjects.

    As for programatically selecting multiple items, if you know the index for each one, you could use the ListBox.SetSelected Method. For example:

    listBox1.SetSelected(1, true);
    listBox1.SetSelected(3, true);
    

    causes listBox1 to select the items at 1 and 3.

    In your case, I would recomend only using the queries to get what values to select (not clearing your listboxes and then just adding back the values that should be selected). Below is my suggestion for how you would rewrite one of your handlers:

    // Store the event handlers in private member variables.
    private System.EventHandler selectedEmployeeChanged = new System.EventHandler(this.lbEmployees_SelectedIndexChanged); 
    
    private void lbProjects_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Declare this outside of try so that we can close it in finally
        DbDataReader reader = null;
        try
        {
            // Remove your event so that updating lbEmployees doesn't cause
            // lbEmployees_SelectedIndexChanged to get fired.
            lbEmployees.SelectedIndexChanged -= selectedEmployeeChanged;
            // Deselect all items in lbEmployees
            lbEmployees.ClearSelected();
    
            cmd.CommandText = "SELECT ProjectName FROM Projects WHERE ProjectID IN(SELECT ProjectID FROM EmployeeProject WHERE EmpId=(SELECT EmpId FROM Employee WHERE EmpName= '@EmpName')) ORDER BY ProjectID";
            cmd.Parameters.AddWithValue("@EmpName", lbProjects.SelectedItem.ToString());
            reader = cmd.ExecuteReader();
    
            // For each row returned, find the index of the matching value
            // in lbEmployees and select it.
            while (rdr.Read())
            {
                int index = lbEmployees.FindStringExact(rdr.GetString(0));
                if(index != ListBox.NoMatches)
                {
                    lbEmployees.SetSelected(index, true);
                }
            }
    
            this.AutoScroll = true;
        }
        finally
        {
            // Ensure that the reader gets closed, even if an exception ocurred
            if(reader != null)
            {
                reader.Close();
            }
            // Ensure that the handler on lbEmployees is re-added,
            // even if an exception was encountered.
            lbEmployees.SelectedIndexChanged += selectedEmployeeChanged;
        }
    }
    

    As a side note, you may want to look into using a JOIN operator in your query string, rather than multiple nested SELECTS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Entity (tables) - Employee & Project. An Employee can have multiple
I am joining two tables Employee and Wages . Now an employee can have
I have two tables, one Company and one Employee: class Company(models.Model): name = models.CharField(max_length=60)
I have two tables: one contains employees information and another is transactions (sales) information
I have two tables an employee table and a project table and I am
I have these two entities. One for Employees: [Table(Name = Employees)] public class Employee
Let's say I have two tables employee and salary with a 1:N relationship (one
I have two tables one Employee and mailing Subscriptions Employee looks like this: Name
UPDATE: Company can have multiple Projects and Company also have Employees. An employee can
Hypothetically I have two tables Employee and Locations. Additionaly I have a view viewEmpLocation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.