Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a grid view on my page and i want to export it to the Excel Sheet, Below is the code i had written to do this task, here i am already passing the dataset to the method to bind the grid and btnExcelExport is the button which will export the Grid Content in to Excel Sheet :-
C#
private void BindGridView(DataSet ds)
    {
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                GVUserReport.DataSource = ds;
                GVUserReport.DataBind();
                btnExcelExport.Visible = true;
            }
        }

    }

    protected void btnExcelExport_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GVUserReport.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();

    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        return;
    }

Now when i m debugging i found that the grid is binded sucessfully but when trying to export it to Excel , i m getting this Error "Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed."
Posted

1 solution

I suppose you are using UpdatePanel in your code. I have faced this issue, below code worked for me. See if it helps you. Add following code in the Page_Load event of your page:

C#
  protected void Page_Load(object sender, EventArgs e) 
  { 
          
     ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); 

     scriptManager.RegisterPostBackControl(this.btnExcelExport);
     // Your Further Code
     ......
}
 
Share this answer
 
Comments
Dharmenrda Kumar Singh 27-Jun-12 3:40am    
Yes it Worked, Thanks a lot :)
Vani Kulkarni 27-Jun-12 3:41am    
Glad it worked! :)
Sandeep Mewara 27-Jun-12 4:39am    
So how long it took you to crack it when you faced it? :)

BTW, 5!
Vani Kulkarni 27-Jun-12 5:02am    
Not too long.. Google was there for my help :) Thanks!
Sandeep Mewara 27-Jun-12 5:16am    
Aaah! Mr. Google...

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