Search code examples
c#datareader

There is already an open DataReader associated with this Command which must be closed first. please help me out


public void StockUpdate()
{
    cmd5 = new SqlCommand("select * from SupplierBillSelection where purordentryid=" + txtPurEntryID.Text + "", con);
    var dr1 = cmd5.ExecuteReader();

    if (dr1.HasRows)
    {
        while (dr1.Read())
        {
             cmd2 = new SqlCommand("select * from Stock where ItemName='" + dr1[2].ToString() + "'", con);
             dr5 = cmd2.ExecuteReader();
            if (dr1.HasRows)
            {
                if (dr5.HasRows)
                {

                    dr5.Read();
                    string insert = "Update Stock set Quantity=" + (Convert.ToSingle(dr5[13]) + Convert.ToSingle(dr1[15])) + " ,TotalPrice=" + (Convert.ToSingle(dr5[14])+Convert.ToSingle(dr1[16]))+ " where ItemName='" + dr1[1].ToString() + "'and CompanyName='" + dr1[2].ToString() +"'";
                    cmd3 = new SqlCommand(insert, con);
                    Console.WriteLine(insert);

Solution

  • Add This "MultipleActiveResultSets=True;" In Connection String.