Vegada Deep
Vegada Deep

Reputation: 1

Unable to Export DataGridView to Excel in VB.NET: Error - Unable to evaluate expression

I'm trying to export a DataGridView (grdFD) to an Excel file in VB.NET. The export function is running without any issues, but I don’t receive the Excel file, and I'm seeing an exception message: {Unable to evaluate expression.}

Here is the function I am using:

 Public Sub ExportGridToExcel(ByVal grdGridView As GridView, ByVal fileName As String)
    Try
        Response.Clear()
        Response.AppendHeader("content-disposition",   String.Format("attachment;filename={0}.xls", fileName))
        Response.Charset = ""
        Response.ContentType = "application/vnd.xls"
        Dim stringWrite As StringWriter = New StringWriter
        stringWrite = New System.IO.StringWriter()
        Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)
        grdGridView.RenderControl(htmlWrite)
        Response.Write(stringWrite.ToString())
        Response.Flush()
        Response.End()
    Catch ex As Exception
        lblMsg.Text = "'" + ex.Message + "'"
    End Try
End Sub

In this code, I am calling the ExportGridToExcel method and passing the grdFD DataGridView and a file name ("TestData"), but I’m not getting the expected Excel file.

I don’t understand why the exception message is {Unable to evaluate expression.}. Could you help me figure out why this is happening? What should I check in the ExportGridToExcel method, and how can I fix this issue?

What I’ve tried so far: I verified that the ExportGridToExcel method is being executed. I checked the grdFD grid and it contains valid data. I’m trying to export the data into an Excel file, but nothing happens, and I don’t see any files.

Upvotes: -1

Views: 27

Answers (0)

Related Questions