Nested Datareader in c#.net
Posted by: Brinda Arumugam
Date: March 04, 2014 06:08AM

Hi,

I'm New to Mysql Database. i have trouble in accessing multi datareader with in single connection.

error occur:
There is already an open DataReader associated with this Connection which must be closed first.

This is my coding
MySqlCommand cmd = new MySqlCommand("select DISTINCT IBillNo,Advv,NetTotal,CName,CPhoneNo,EID from InvoiceDetails where DStatus='D'", con);
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Double NetA = 0, Adv = 0;
if (dr[2] == DBNull.Value || dr[2].ToString() == null)
{
NetA = 0;
}
else
{
NetA = Convert.ToDouble(dr[2].ToString());
}

if (dr[1] == DBNull.Value || dr[1].ToString() == null)
{
Adv = 0;
}
else
{
Adv = Convert.ToDouble(dr[1].ToString());
}
Double Pnt = 0;
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();

if ((NetA - (Adv + Pnt)) > 0)
{
if (dr["EID"] == DBNull.Value || dr["EID"].ToString() == null || dr["EID"].ToString() == "")
{
int x = PaymentLoop.Rows.Add();
PaymentLoop.Rows[x].Cells[0].Value = dr[0].ToString();
PaymentLoop.Rows[x].Cells[1].Value = dr[3].ToString() + " -- " + dr[4].ToString();
PaymentLoop.Rows[x].Cells[2].Value = NetA.ToString();
PaymentLoop.Rows[x].Cells[3].Value = (NetA - (Adv + Pnt)).ToString();
}
}
}
cmd.Dispose(); dr.Dispose();
PaymentLoop.ClearSelection();


Let me know the solution...

Options: ReplyQuote


Subject
Written By
Posted
Nested Datareader in c#.net
March 04, 2014 06:08AM


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.