Solved

how to fix web method error: Thread was being aborted: HttpContext.Current.Response.[End]()

Posted on 2012-03-17
3
2,462 Views
Last Modified: 2012-04-10
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.Response.[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:

    <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

Open in new window

0
Comment
Question by:njgroup
3 Comments
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 37734122
'While using update panel , Response.end makes this problem . You can skip that exception by the below way .

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(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]()
return ""
catch ex as exception
 if (instr(lcase(ex.message),"thread was being aborted") then
return "" 
end if
end try
end sub
0
 

Author Comment

by:njgroup
ID: 37734148
same error, even in try catch, the error is thrown !
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37734950
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question