troubleshooting Question

Export Gridview to Excel in

Avatar of whiwex
whiwex asked on
.NET ProgrammingVisual Basic.NETMicrosoft Development
14 Comments1 Solution573 ViewsLast Modified:
I have a gridview call gvfamilies on a page with a button called button1. I populate the gridview and I want to export the contents of the gridview to an excel file called test.xls
I have searched the internet and have found several examples of code but I can't seem to get any to work. Can you tell me what is wrong with the following code. I get an error message of
Microsoft JScript runtime 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 '<div>
      <table cells'.

Here is my code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call ExportToExcel("test.xls", gvFamilies)
    End Sub
    Private Sub ExportToExcel(ByVal strFileName As String, ByVal dg As GridView)

        Response.AddHeader("content-disposition", "attachment;filename=Demand_Report_" & Date.Today.ToString("MM-dd-yyyy") & ".xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.xls"
        Dim StringWriter As New System.IO.StringWriter()
        Dim HtmlTextWriter As New HtmlTextWriter(StringWriter)
        Response.Write(StringWriter.ToString())    'This is where it errors

    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)

    End Sub
Join our community to see this answer!
Unlock 1 Answer and 14 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros