Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
hi friends,

In my form, lot of combo box is there. I want to load different table datas to combo box. I am trying to do that. but code is going on very long. Because of the connection open and close codings.

when i run two command in without close connection and open it throw

>
There is already an open DataReader associated with this Connection which must be closed first.<br />

Exception.

how to remove this open and close in my program

string MyConString = ConfigurationManager.ConnectionStrings["College_Management_System.Properties.Settings.cmsConnectionString"].ConnectionString;
          MySqlConnection connection = new MySqlConnection(MyConString);
          MySqlCommand command = connection.CreateCommand();
          MySqlDataReader Reader;
          command.CommandText = "select name from course_master";
          connection.Open();
          Reader = command.ExecuteReader();
          while (Reader.Read())
          {
              cmbo_course.Items.Add(Reader[0].ToString());
          }

          command.CommandText = "select name from country_master";
          Reader = command.ExecuteReader();
          while (Reader.Read())
          {
              cmbo_perCountry.Items.Add(Reader[0].ToString());
              cmbo_country.Items.Add(Reader[0].ToString());
          }
          connection.Close();


thanks in advance..
Posted
Updated 31-May-11 2:26am
v3

In your ConnectionString add another attribute called as MultipleActiveResultSets=true;

through MARS you can implement asynchronous communication.
add this attribute then you will not face the problem.
 
Share this answer
 
Comments
Sagotharan Jagadeeswaran 31-May-11 6:59am    
Is MARS (Multiple Active Result Sets) supported in mysql?
Well, it's because DataReaders are connected-mode, forward only thing. As long as a reader is open, connection is active and working on it. Thus if you try to use another reader then, you get the error.

Have a look at these:
MSDN blog describing the same in much more detail[^]
Workaround suggested with implication[^]
Similar discussion[^]
 
Share this answer
 
to solve this first you have to close the reader that is already opened by rdr.close()

that is after

while(Reader.read())
{
}

Reader.close();

then use second reader

or else

follow the steps suggested by Mr.S Mewara
 
Share this answer
 
Comments
Sagotharan Jagadeeswaran 1-Jun-11 1:26am    
thank u very much@!/
karan joshua 1-Jun-11 6:34am    
your most welcome......

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