Exception: Thread was being aborted

Discussion in 'ASP .Net' started by Jeff, Dec 16, 2005.

  1. Jeff

    Jeff Guest

    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
    Jeff, Dec 16, 2005
    #1
    1. Advertising

  2. Jeff. I ran into the same issue not long ago, it is documented here

    http://support.microsoft.com/default.aspx?scid=kb;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" <> wrote in message
    news:...
    >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
    >
    Jose Rodriguez, Dec 16, 2005
    #2
    1. Advertising

  3. Jeff

    Jeff Guest

    Thanks Jose!
    Jeff, Dec 16, 2005
    #3
  4. Jeff

    sebichondo

    Joined:
    Feb 3, 2011
    Messages:
    1
    Thanks, saved me hours of debugging!!
    sebichondo, Feb 3, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Miller
    Replies:
    3
    Views:
    3,950
    Stephen Miller
    Jul 2, 2004
  2. Johanna
    Replies:
    0
    Views:
    595
    Johanna
    Oct 13, 2004
  3. =?Utf-8?B?Sm9oYW5uYQ==?=
    Replies:
    3
    Views:
    1,943
    =?Utf-8?B?Sm9oYW5uYQ==?=
    Oct 15, 2004
  4. Alan Silver
    Replies:
    5
    Views:
    23,044
    Alan Silver
    Dec 6, 2005
  5. darrel
    Replies:
    5
    Views:
    5,904
    darrel
    Mar 9, 2006
Loading...

Share This Page