Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to bind data with 2 different combo box for that i had use sql data reader twice time for bind data from 2 different table but it shows an error
C#
SqlConnection conn = new SqlConnection("Data Source=HP\\SASI;Initial Catalog = test;Integrated Security=SSPI;");
        conn.Open();
        SqlCommand cmd = new SqlCommand();
        string query = "Select fn+''+ln from user_details where category='Doctor'";// position column from position table
        cmd.Connection = conn;
        cmd.CommandText = query;

        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            string myItem = dr[0].ToString();
            comboBox1.Items.Add(myItem);
        }
        conn.Close();

      SqlConnection conn1 = new SqlConnection("Data Source=HP\\SASI;Initial Catalog = test;Integrated Security=SSPI;");
        conn1.Open();
        SqlCommand cmd1 = new SqlCommand("select firstname+''+lastname from patient_table",conn1);
        SqlDataReader dr1 = cmd1.ExecuteReader();
        while (dr1.Read())
        {
            string name = dr[0].ToString();
            comboBox1.Items.Add(name);
        }
Posted
Updated 5-Jun-14 0:26am
v3
Comments
[no name] 5-Jun-14 6:29am    
And are we supposed to guess which error it is that you are seeing?
Thanks7872 5-Jun-14 8:24am    
Its obvious that error should be :There is already an open DataReader associated with this Connection
[no name] 5-Jun-14 10:31am    
No in fact it's not "obvious" at all. It could 486,245,346,435 other error messages that you could guess at where an "Already open data reader" is but one.
Thanks7872 5-Jun-14 12:26pm    
You can see the accepted solution below and now i think you know why its accepted :)

1 solution

Its very simple but i recommend you to read this. It will help you alot. For this particular issue, refer Closing the DataReader section there.

Retrieving Data Using a DataReader[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900