Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How to avoid an error (XML page cannot be displayed) when exporting GridView records to an Excel file using ASP.Net 4.0 with VS 2010 in C#?

I am creating my first ASP.NET web application.
I try to export a GridView to Excel using the following code but I get the following error once I click on the command button:
------------------------------------------------
The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
The operation completed successfully. Error processing resource 'http://localhost:4380/default.aspx'. Line 6, Position 95

   <td>5455</td><td>10/3/2013</td><td>10/3/2013</td><td>New Rejects from current d...
------------------------------------------------------------
The C# code for my command button to Export the GridView records to Excel is as follows:

 protected void Button1_Click1(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.AppendHeader("contenet-disposition", "attachment; filename-Clients.xls");
            Response.ContentType = "application/excel";

            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            GridView1.RenderControl(htmlTextWriter);
            Response.Write(stringWriter.ToString());
            Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }
Avatar of QuinnDex
QuinnDex

the error is in your XSL style sheet, check that for correct mapping
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial