Exception: Thread was being aborted

J

Jeff

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
 

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

Top