Re: Nested Datareader in c#.net
Posted by: Fernando Gonzalez Sanchez
Date: March 04, 2014 10:57AM

Hi, you cannot have more than one reader being processed with a single connection (this is a limitation on pretty much all ADO.NET drivers, not only MySql Connector/NET).

Solution is to open a 2nd connection like this:

Double Pnt = 0;
MySqlConnection con2 = new MySqlConnection( con.ConnectionString );
con2.Open();
try {

MySqlCommand pay = new MySqlCommand("select * from PaymentDetails where IBillNo='" + dr[0].ToString() + "'", con);
MySqlDataReader drpay = pay.ExecuteReader();
while (drpay.Read())
{
if (drpay["Payment"] == DBNull.Value || drpay["Payment"].ToString() == null)
{
Pnt = 0;
}
else
{
Pnt = Pnt + Convert.ToDouble(drpay["Payment"].ToString());
}
}
drpay.Dispose();
pay.Dispose();

finally {
if( ( con2.State & ConnectionState.Open ) != 0 ) con2.Close();
}


This fixes the problem.

Now, there still a small performance issue, because the code is opening/closing a connection in the main loop (con2), so better move the con2 creation/opening at the beginning of the code, and the closing at the end.

Or you can also enable pooling, so the connections are not physically created/opened, every time MySqlConnection.Open is called.

*** My opinions do not necessarily reflect the opinions of my employeer ***
Fernando Gonzalez Sanchez
Sr. Software Engineer
MySql Connector/NET Team
Oracle Corporation

Options: ReplyQuote


Subject
Written By
Posted
Re: Nested Datareader in c#.net
March 04, 2014 10:57AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.