I'm trying to create a csv file bases on the contents of a gridview when a user clicks a button. However I keep getting Connection was reset by server when clicking the button. Thanks in advance.
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
Try
Dim out As IO.TextWriter = Response.Output
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=SearchResults.csv")
Response.Charset = ""
Response.ContentType = "application/text"
gvFiles.AllowPaging = False
gvFiles.DataBind()
Me.EnableViewState = False
'Bind DataTable to GridView
'Search() method returns a DataTable of search results
'gvFiles.DataSource = Search()
'gvFiles.DataBind()
'String builder class to add row data
Dim sb As New StringBuilder()
For k As Integer = 0 To gvFiles.Columns.Count - 1
'add separator
sb.Append(gvFil开发者_运维技巧es.Columns(k).HeaderText & ",")
Next
'append(New line)
sb.Append(vbCrLf)
'Get Rows
For i As Integer = 0 To gvFiles.Rows.Count - 1
'Get columns
For k As Integer = 0 To gvFiles.Columns.Count - 1
'add separator
sb.Append(gvFiles.Rows(i).Cells(k).Text & ",")
Next
'append new line
sb.Append(vbCrLf)
Next
out.Write(sb.ToString())
out.Flush()
'out.Close()
Response.End()
Catch ex As Exception
Response.Write("Unable to generate File" & ex.ToString())
End Try
End Sub
I found the answer, I had to edit the web.config connection timeouts etc. because as it turns out it was generating such a large .csv file that the connection was timing out.
精彩评论