Export to PDF Error: RegisterForEventValidation can only be called during Render

Last Reply one month ago By dharmendr

Posted one month ago

I want to generate pdf.

Error:-System.InvalidOperationException: 'RegisterForEventValidation can only be called during Render();'

    public partial class WebForm28 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Mycon"].ToString());

        protected void Page_Load(object sender, EventArgs e)
        {
            Label2.Text = Request.QueryString["orderid"];
            Label3.Text = Label3.Text;
            findorderdate(Label3.Text);
            findaddress(Label3.Text);
            showgrid(Label3.Text);
        }

        [Obsolete]
        protected void Button1_Click(object sender, EventArgs e)
        {
            exportpdf();
        }

        [Obsolete]
        private void exportpdf()
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=OrderInvoice.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Panel1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
        }
        private void findorderdate(String Orderid)
        {
            String myquery = "UPDATE AddRaw_199 SET status = 1 WHERE Raw_M='Potato' and Id='" + Orderid + "'";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = myquery;
            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables.Count > 0)
            {

                Label3.Text = ds.Tables[0].Rows[0]["dateoforder"].ToString();

            }

            con.Close();
        }
        private void findaddress(String Orderid)
        {
            String myquery = "UPDATE AddRaw_199 SET status = 1 WHERE Raw_M='Potato' and Id='" + Orderid + "'";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = myquery;
            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables.Count > 0)
            {

                Label5.Text = ds.Tables[0].Rows[0]["Address"].ToString();

            }

            con.Close();
        }
        private void showgrid(String orderid)
        {
            DataTable dt = new DataTable();
            DataRow dr;

            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.Columns.Add("Email");
            dt.Columns.Add("State");
            dt.Columns.Add("District");
            dt.Columns.Add("PQuantity");
            dt.Columns.Add("PoQuantity");
            dt.Columns.Add("Total");
             String myquery = "UPDATE AddRaw_199 SET status = 1 WHERE Raw_M='Potato' and Id='" + orderid + "'";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = myquery;
            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            int totalrows = ds.Tables.Count;
            int i = 0;
            int grandtotal = 0;
            while (i < totalrows)
            {
                dr = dt.NewRow();
                dr["Id"] = ds.Tables[0].Rows[i]["Id"].ToString();
                dr["Name"] = ds.Tables[0].Rows[i]["Name"].ToString();
                dr["Email"] = ds.Tables[0].Rows[i]["Email"].ToString();
                dr["State"] = ds.Tables[0].Rows[i]["State"].ToString();
                dr["District"] = ds.Tables[0].Rows[i]["State"].ToString();
                dr["PQuantity"] = ds.Tables[0].Rows[i]["PQuantity"].ToString();

                dr["PoQuantity"] = ds.Tables[0].Rows[i]["PoQuantity"].ToString();
                int price = Convert.ToInt16(ds.Tables[0].Rows[i]["PQuantity"].ToString());
                int quantity = Convert.ToInt16(ds.Tables[0].Rows[i]["PoQuantity"].ToString());
                int totalprice = price * quantity;
                dr["Total"] = totalprice;
                grandtotal = grandtotal + totalprice;
                dt.Rows.Add(dr);
                i = i + 1;
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();

            Label6.Text = grandtotal.ToString();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
    }