thread was being aborted

D

DC Gringo

I have some code that creates and writes to an excel file. Right as I
response.end() to avoid HTML in the Excel document, I get an exception:

---------------

An attempt to log the following error
failed:System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort(Object stateInfo)
at System.Web.HttpResponse.End()

-----------------

Here's my code


Public Sub btnShasExcel_OnClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Try
Dim ds As New DataSet
Dim da As New SqlDataAdapter(Session("savedShasSql"),
connection1.conString)
da.Fill(ds, "ShasExcel")
Dim dt As DataTable = ds.Tables("ShasExcel")

'This code was dumping html into the spreadsheet
'Response.ContentType = "application/ms-excel"
'Response.AddHeader("Content-Disposition", "inline;filename=shas.xls")
'Response.Write(ConvertDtToTDF(dt))

'This is the new code
Response.ContentType = "application/ms-excel"
Response.AddHeader("Content-Disposition", "inline;filename=shas.xls")
Response.Clear()
Response.Write(ConvertDtToTDF(dt))
Response.End()

Catch ex As Exception
ExceptionManager.Publish(ex)
End Try

End Sub



Private Function ConvertDtToTDF(ByVal dt As DataTable) As String

Try

Dim dr As DataRow, ary() As Object, i As Integer
Dim iCol As Integer

'Output Column Headers
For iCol = 0 To dt.Columns.Count - 1
Response.Write(dt.Columns(iCol).ToString & vbTab)
Next

Response.Write(vbCrLf)

'Output Data

For Each dr In dt.Rows
ary = dr.ItemArray
For i = 0 To UBound(ary)
Response.Write(ary(i).ToString & vbTab)
Next
Response.Write(vbCrLf)
Next

Catch ex As Exception
ExceptionManager.Publish(ex)

End Try

End Function

_____
DC G
 
D

Daniel Walzenbach

Gringo,

you might want to replace Response.End() with Response.Flush.

Let me know if this works for you.

Daniel Walzenbach
 
D

DC Gringo

Daniel,

Sorry, it didn't work. I didn't get the error, but the HTML returned to the
Excel file. Putting the response.flush() after the response.close() or vice
versa still generated the error.

_____
DC G
 
B

Brock Allen

Yes, this is the way ASP.NET attempts to insure that the processing halts
for the current request -- they throw a ThreadAbortException. Sort of odd,
eh? Well, the magic with a ThreadAbortException is that if you put a try/catch
around it, it still gets thrown outside your catch -- they're doing this
so you don't catch their attempt to terminate the request. So, in short,
it's just how it works.
 
B

Bruce Barker

a Response.End() does a flush, then kills the current thread (to stop
continued processing). ignore the thread abort in your catch

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,748
Members
45,545
Latest member
rapter____0

Latest Threads

Top