njgroup
asked on
how to fix web method error: Thread was being aborted: HttpContext.Current.Response.[End]()
hi,
I have web method to download csv file (from datatable) using jquery ajax
now everything is fine, but when the code reached to line:
HttpContext.Current.Respon se.[End]()
its exiting the method, and returning this error message in firebug: Thread was being aborted.
I tried to remove this line, but I didn't get the file to be saved,
here is my code:
I have web method to download csv file (from datatable) using jquery ajax
now everything is fine, but when the code reached to line:
HttpContext.Current.Respon
its exiting the method, and returning this error message in firebug: Thread was being aborted.
I tried to remove this line, but I didn't get the file to be saved,
here is my code:
<WebMethod()> _
Public Shared Function ExportReportAjax(ByVal ExportType As Integer, ByVal DataToExport As String) As String
Dim clientID As String = HttpContext.Current.Session("ClientID")
Dim strCheck As String = "Select Client_ClassID from dbo.CTS_Clients where Client_ID= " & clientID
Dim dsCheck As DataSet = clsDB.getData(strCheck)
Dim response As HttpResponse = HttpContext.Current.Response
ExportToSpreadsheet(dsCheck.Tables(0), "asfas")
Return ""
End Function
Public Shared Sub ExportToSpreadsheet(ByVal table As DataTable, ByVal name As String)
Dim context As HttpContext = HttpContext.Current
context.Response.Clear()
For Each column As DataColumn In table.Columns
context.Response.Write(column.ColumnName + ";")
Next
context.Response.Write(Environment.NewLine)
For Each row As DataRow In table.Rows
For i As Integer = 0 To table.Columns.Count - 1
context.Response.Write(row(i).ToString().Replace(";", String.Empty) & ";")
Next
context.Response.Write(Environment.NewLine)
Next
context.Response.ContentType = "text/csv"
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" & name & ".csv")
context.Response.[End]()
End Sub
ASKER
same error, even in try catch, the error is thrown !
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Public Shared Sub ExportToSpreadsheet(ByVal table As DataTable, ByVal name As String)
try
Dim context As HttpContext = HttpContext.Current
context.Response.Clear()
For Each column As DataColumn In table.Columns
context.Response.Write(col
Next
context.Response.Write(Env
For Each row As DataRow In table.Rows
For i As Integer = 0 To table.Columns.Count - 1
context.Response.Write(row
Next
context.Response.Write(Env
Next
context.Response.ContentTy
context.Response.AppendHea
context.Response.[End]()
return ""
catch ex as exception
if (instr(lcase(ex.message),"
return ""
end if
end try
end sub