Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have requirement i.e export to excel gridview data in the master pages. While i am trying to export i am getting sys.webforms.pagerequestmanagerparsererrorexception error. Any one please help me out regarding this issue. I have tried in different ways like as follows
1.
DataTable dt = BindCompanies();
        string attachment = "attachment; filename=Employee.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/vnd.ms-excel";
        string tab = "";
        foreach (DataColumn dc in dt.Columns)
        {
            Response.Write(tab + dc.ColumnName);
            tab = "\t";
        }
        Response.Write("\n");
        int i;
        foreach (DataRow dr in dt.Rows)
        {
            tab = "";
            for (i = 0; i < dt.Columns.Count; i++)
            {
                Response.Write(tab + dr[i].ToString());
                tab = "\t";
            }
            Response.Write("\n");
        }
        Response.End();


2.
string attachment = "attachment; filename=Export.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htw);

//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();


Regards,
Jeevan.
Posted

C#
protected void Export2Excel()
{
try
{
    this.GridView1.AllowPaging = false;
    this.GridView1.AllowSorting = false;
    this.GridView1.EditIndex = -1;

    // Let's bind data to GridView
    this.BindData();

    // Let's output HTML of GridView
    Response.Clear();
    Response.ContentType = "application/vnd.xls";
    Response.AddHeader("content-disposition",
            "attachment;filename=MyList.xls");
    Response.Charset = "";
    StringWriter swriter = new StringWriter();
    HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
    GridView1.RenderControl(hwriter);
    Response.Write(swriter.ToString());
    Response.End();
}
catch (Exception exe)
{
    throw exe;
}

}       


use this code and try it it will work for me...

and also take care that you need not put your grid in side a update panel or it will resulted in error like this...

C#
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
 
Share this answer
 
Comments
Jeevanrao 4-Nov-11 6:11am    
Hi,

When i am trying to export using this i am getting following error.
data = Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack. Can you please help me out.
 
Share this answer
 
Could you please go through the link [^]. This is a very handy in this case.
 
Share this answer
 

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