469,902 Members | 1,813 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

Exception: Thread was being aborted

I have some code that enables users to download the contents of a DataGrid
as an Excel file. The following code snippet is the relevant part. It works
just fine - meaning specifically that the downloaded Excel file is what I
want.

The problem is that every time this code runs, my "catch all" exception
handler (an HTTP Module) catches the "Thread was being aborted" exception
(from System.Threading.Thread.AbortInternal()).

What can I do to prevent that exception from being thrown? You can see that
the following code uses Response.End. is there a better way?
------------------- start of code snippet ---------------------

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

response.Clear();
response.Charset = "";
response.ContentEncoding = System.Text.Encoding.UTF8;
response.Charset = "";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +
filename + "\"");

// get the text of the rendered datagrid
string dgText;
using (StringWriter sw = new StringWriter())
{
using (System.Web.UI.HtmlTextWriter htw = new
System.Web.UI.HtmlTextWriter(sw))
{
// instantiate a datagrid
dg.RenderControl(htw);
dgText = sw.ToString();
}
}

// set the response mime type
response.ContentType = "application/vnd.ms-excel";
response.Write(dgText);
response.End();

------------------- end of code snippet ---------------------
Any ideas?

Thanks
Dec 16 '05 #1
2 4994
Jeff. I ran into the same issue not long ago, it is documented here

http://support.microsoft.com/default...b;en-us;312629

on my exception class, I added this code to bypass it

If AppException.GetBaseException.GetType.Name = "ThreadAbortException" Then

Return

End If

hth

Jose
"Jeff" <Je**@NoSpam.com> wrote in message
news:OA*************@TK2MSFTNGP12.phx.gbl...
I have some code that enables users to download the contents of a DataGrid
as an Excel file. The following code snippet is the relevant part. It works
just fine - meaning specifically that the downloaded Excel file is what I
want.

The problem is that every time this code runs, my "catch all" exception
handler (an HTTP Module) catches the "Thread was being aborted" exception
(from System.Threading.Thread.AbortInternal()).

What can I do to prevent that exception from being thrown? You can see
that the following code uses Response.End. is there a better way?
------------------- start of code snippet ---------------------

System.Web.HttpResponse response =
System.Web.HttpContext.Current.Response;

response.Clear();
response.Charset = "";
response.ContentEncoding = System.Text.Encoding.UTF8;
response.Charset = "";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +
filename + "\"");

// get the text of the rendered datagrid
string dgText;
using (StringWriter sw = new StringWriter())
{
using (System.Web.UI.HtmlTextWriter htw = new
System.Web.UI.HtmlTextWriter(sw))
{
// instantiate a datagrid
dg.RenderControl(htw);
dgText = sw.ToString();
}
}

// set the response mime type
response.ContentType = "application/vnd.ms-excel";
response.Write(dgText);
response.End();

------------------- end of code snippet ---------------------
Any ideas?

Thanks

Dec 16 '05 #2
Thanks Jose!
Dec 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jan | last post: by
5 posts views Thread by Lucvdv | last post: by
1 post views Thread by R.A.M. | last post: by
10 posts views Thread by =?Utf-8?B?ZHNoZW1lc2g=?= | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.