Question multiple DataReaders

Nuha_IT's Avatar, Join Date: May 2011
Newbie Member
Salam,

I am working on a project (windows application), I have used 2 SqlDataReaders in each event of 2 comboBoxes; where each comboBox display either the name of employees or their ID number.
I have also used TextChange event, so if I selected one of the ID the name appear in the other comboBox and vice versa.



this is the code:

Code:
private void comboBox2_TextChanged(object sender, EventArgs e)
        {
            SqlCommand Sc4 = new SqlCommand("select emp_id from employee where emp_fname +' '+ emp_sname +' '+ emp_lname='" + comboBox2.SelectedItem + "'", cn);

            if (cn.State == ConnectionState.Closed)
                cn.Open();
            SqlDataReader rdr = Sc4.ExecuteReader();
            if (rdr.Read())
            {
                comboBox3.Text = rdr["emp_id"].ToString().Trim();
                rdr.Close();
                cn.Close();
            }
            else
            {
                //MessageBox.Show("Not found");
                rdr.Close();
            }
        }

Code:
        private void comboBox3_TextChanged(object sender, EventArgs e)
        {
            SqlCommand Sc5 = new SqlCommand("select emp_fname+' '+ emp_sname+' '+ emp_lname as en from employee where emp_id=" + comboBox3.SelectedItem, cn);

            if (cn.State == ConnectionState.Closed)
                cn.Open();
            SqlDataReader rdr2 = Sc5.ExecuteReader();
            if (rdr2.Read())
            {
                comboBox2.Text = rdr2["en"].ToString().Trim();
                rdr2.Close();
                cn.Close();
            }
            else
            {
                //MessageBox.Show("Not found");
                rdr2.Close();
            }
        }
Nuha_IT's Avatar, Join Date: May 2011
Newbie Member
Using only one of the readers works perfectly, but when I use both of readers the following message appear:

There is already an open datareader associated with this command which must be closed first

FYI

I have tried to use different connection for each reader, also used MARS = True; in the connection string but non of the solutions worked.

Got any ideas?

Note: the picture is in the attachment
Attached Images
File Type: png QKb55568.png (2.6 KB, 2 views)
peterpaulrubens2011's Avatar, Join Date: Jun 2011
Newbie Member
Hey,

I think a solution of your problem. Your problem is actually parallel text change event on a same table.
You can prevent it by using got focus or click event of each combo-box, just use a variable like isCombobox2Working when user clicks on combox2 assign isCombobox2Working = true and then in textchange event check if the isCombobox2Working = true then work. When user clicks on Combobox3 set isCombobox2Working = false and in TextChanged event for combox3 check to work only isCombobox2Working = false.
tzuhsun's Avatar, Join Date: Jun 2011
Newbie Member
That is obvious, you are using same connection object between two different dataReaders.

You can try peterpaulrubens2011 way to run only one datareader at one time, or use two different connection objects for each datareader.

Code:
private void comboBox3_TextChanged(object sender, EventArgs e)
        {
            SqlCommand Sc5 = new SqlCommand("select emp_fname+' '+ emp_sname+' '+ emp_lname as en from employee where emp_id=" + comboBox3.SelectedItem, cn);

            if (cn.State == ConnectionState.Closed)
                cn.Open();
            SqlDataReader rdr2 = Sc5.ExecuteReader();
            if (rdr2.Read())
            {
                comboBox2.Text = rdr2["en"].ToString().Trim();
                rdr2.Close();
                cn.Close();
            }
            else
            {
                //MessageBox.Show("Not found");
                rdr2.Close();
            }
        }