Click here to Skip to main content
15,919,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends..
I am trying to export my Gridview data to Excel sheet.

Here is my code..
protected void Page_Load(object sender, EventArgs e)
{
    con.Open();
    if (!IsPostBack)
    {
       BindData();
    }
    con.Close();
        
}

private void BindData()
{
    string sql = "select * from Student";
    SqlDataAdapter adpter = new SqlDataAdapter(sql, con);
    DataSet ds = new DataSet();
    adpter.Fill(ds);

    MyGrid.DataSource = ds;
    MyGrid.DataBind();
}

protected void bnt_Click(object sender, EventArgs e)
{

    Response.Clear();
    Response.AddHeader("content-disposition",   "attachment;filename=FileName.xls");
    Response.Charset = "";
 
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";
    this.EnableViewState = false;
    System.IO.StringWriter SW = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter HTW = new HtmlTextWriter(SW);
   
    MyGrid.RenderControl(HTW);
    Response.Write(SW.ToString());
    Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
}



But when I run this .., I get the following error..

Sys.WebForms.PageRequestManagerParserErrorException:The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), Response Filters, httpmodules or server trace is enabled.
Details: Error parsing near '

Posted

When you get an error like this, that you have never seen before, the best course of action is to paste the complete error message into the Google search box.

That is what I did for the error you are getting and one of the hits was for someone having what looks to me like the exact same problem that you are having. Here[^] is the link. Have a look at the suggested solutions (particularly the second one, looks good to me).

If that doesn't work then I suggest that you do as I suggest in my first paragraph and work your way through the list.

Good luck! :)
 
Share this answer
 
Try to read this article first:

Using C# to Create an Excel Document[^]

It shows how to export data to excel spreadsheet programatically.

Then all that you got to do is iterate throught dataset records to send data one by one using
VB
addData(int row, int col, string data,
            string cell1, string cell2,string format)
 
Share this answer
 
v2

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