Click here to Skip to main content
15,898,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code that is in click event of a button. When I click the button then it takes much time and it shows the following message.
C#
Server Error in '/ERP' Application.
ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been initialized.

My code is as below:

C#
protected void btnSave_Click(object sender, EventArgs e)
    {
        //lblErrmsg.Text = "Saving.Please wait....";
        SqlTransaction transaction;
        cn.Open();
        transaction = cn.BeginTransaction();
        try
        {
            SqlCommand cmdd = new SqlCommand("BOM_PO_Delete", cn, transaction);
            cmdd.CommandType = CommandType.StoredProcedure;
            cmdd.Parameters.AddWithValue("@Styleid", lblStyleid.Text);
            cmdd.ExecuteNonQuery();
            for (int i = 0; i < grdPODtl.Rows.Count; i++)
            {
                CheckBox chkselect = (CheckBox)grdPODtl.Rows[i].FindControl("chkselect");
                if (chkselect.Checked == true)
                {
                    Label lbllot = (Label)grdPODtl.Rows[i].FindControl("lbllot");
                    Label lblqty = (Label)grdPODtl.Rows[i].FindControl("lblqty");
                    SqlCommand cmddl = new SqlCommand("BOM_PO_Save", cn, transaction);
                    cmddl.CommandType = CommandType.StoredProcedure;
                    cmddl.Parameters.AddWithValue("@Styleid", lblStyleid.Text);
                    cmddl.Parameters.AddWithValue("@Lot", lbllot.Text);
                    cmddl.Parameters.AddWithValue("@qty", lblqty.Text);
                    cmddl.ExecuteNonQuery();
                }
            }
            SqlCommand cmdd2 = new SqlCommand("BOM_PO_Update", cn, transaction);
            cmdd2.CommandType = CommandType.StoredProcedure;
            cmdd2.CommandTimeout = 400;
            cmdd2.Parameters.AddWithValue("@styleid", lblStyleid.Text);
            cmdd2.ExecuteNonQuery();
            transaction.Commit();
            lblErrmsg.Text = "Saved Successfully";
        }
        catch (Exception ex)
        {
            lblErrmsg.Text = ex.Message;
            transaction.Rollback();
        }
        finally
        {
            transaction.Dispose();
            cn.Close();
        }         
    }
Posted
Updated 19-Dec-15 18:02pm
v2
Comments
OriginalGriff 20-Dec-15 3:07am    
You sure you posted the right code?
The error message is talking about ExecuteReader, and your code only uses ExecuteNonQuery

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