Error "Property Evaluation Failed" for "Response.End" when exporting datatable to excel

Posted by Krishnasamy2008 under ASP.NET on 8/16/2011 | Points: 10 | Views : 5668 | Status : [Member] | Replies : 1
Hi Friends,

I got the error of "Property Evaluation Failed" for the sentence "Response.End" in the following code.

Private Sub ExportToExcel(ByVal dt As DataTable, ByVal response As HttpResponse, ByVal fileName As String)

'instantiate a datagrid
Dim dg As New DataGrid
'create a string writer
Dim stringWrite As New System.IO.StringWriter
'create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)

Try
'first let's clean up the response.object
response.Clear()
response.Charset = ""
'set the response mime type for excel
response.ContentType = "application/vnd.ms-excel"
response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
'set the datagrid datasource to the dataset passed in
dg.DataSource = dt
'bind the datagrid
dg.DataBind()
'tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite)
'all that's left is to output the html
response.Write(stringWrite.ToString)
response.End()
Catch ex As Exception
Throw ex
Finally
dg.Dispose()
End Try

End Sub


Thanks and Regards,
Krishna.K




Responses

Posted by: Itorian on: 8/16/2011 [Member] Starter | Points: 25

Up
0
Down
Your error msg not saying the exact issue, do one thing, place the breakpoint and find the actual bug or point of issue.

Student of M.Tech IT | Microsoft MVP | Mindcracker MVP | IT Faculty | Blogger | Author

Krishnasamy2008, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response