4

I have a DataTable which is bound to a GridView. I also have a button that when clicked exports the DataTable to an Excel file. However, the following error is occuring:

ErrMsg = "Thread was being aborted."

Here is part of the code where the error is being thrown:

private static void Export_with_XSLT_Web(DataSet dsExport, 
                                         string[] sHeaders,
                                         string[] sFileds, 
                                         ExportFormat FormatType,
                                         string FileName)
{
    try
    {
        // Appending Headers
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;

        if(FormatType == ExportFormat.CSV)
        {
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AppendHeader("content-disposition",
                                                      "attachment; 
                                                      filename=" + FileName);
        }
        else
        {
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.AppendHeader("content-disposition", 
                                                      "attachment; 
                                                      filename=" + FileName);
        }

        // XSLT to use for transforming this dataset.                       
        MemoryStream stream = new MemoryStream();
        XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);

        CreateStylesheet(writer, sHeaders, sFileds, FormatType);
        writer.Flush();
        stream.Seek(0, SeekOrigin.Begin);

        XmlDataDocument xmlDoc = new XmlDataDocument(dsExport);
        //dsExport.WriteXml("Data.xml");
        XslTransform xslTran = new XslTransform();
        xslTran.Load(new XmlTextReader(stream), null, null);

        using(StringWriter sw = new StringWriter())
        {
            xslTran.Transform(xmlDoc, null, sw, null);

            //Writeout the Content              
            HttpContext.Current.Response.Write(sw.ToString());                
            writer.Close();
            stream.Close();
            HttpContext.Current.Response.End();
        }
    }
    catch(ThreadAbortException Ex)
    {
        string ErrMsg = Ex.Message;
    }
    catch(Exception Ex)
    {
        throw Ex;
    }
    finally
    {

    }
}

After changing HttpContext.Current.Response.End to HttpContext.Current.ApplicationInstance.CompleteRequest, it now just goes to the finally block and I can't figure out what error message is being thrown.

Xaisoft
  • 45,655
  • 87
  • 279
  • 432

5 Answers5

9

The ThreadAbortException is thrown from the following line:

HttpContext.Current.Response.End();

Here's more details and a workaround.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
5

I tried using HttpContext.Current.ApplicationInstance.CompleteRequest. It did work but also exported the complete HTML Code of the page at the end of the downloaded file, which was undesirable.

Response.BuffferOutput = True;
Response.Flush();
Response.Close();

Then after a hell of effort, I bumped into the above code. And it works perfectly without any exception or undesirable code at the end of the downloaded file.

Source

Viral Jain
  • 1,004
  • 1
  • 14
  • 30
  • Thanks Viral for ur help.It work for me.Thanks for saving my time.Instead of response.end() I wrote following 3 lines.Response.BuffferOutput = True; Response.Flush(); Response.Close(); – Jui Test Feb 29 '16 at 05:39
0

I had the different story here, I am using telerik controls and NONE helped me, eventually I came to know that I had to just wrap my markup in telerik:RadAjaxPanel, below is the code snippet to disable ajax on RequestStart Client side event handler.

<telerik:RadAjaxPanel runat="server" 
    ClientEvents-OnRequestStart="AjaxRequestStart">

and then in my user control, I added just one more function as depicted below:

<script type="text/javascript">
    function AjaxRequestStart(target, arguments) {
        arguments.set_enableAjax(false);
}

ablaze
  • 722
  • 7
  • 30
0

The exception was caused by Response.End

One thing you can do is to logically trap the exception message...

this method will not affect the data but it will only trap the exception...

Try

    .........(codes here)
    Response.End

Catch ex as Message

    If Not ex.Message = "Thread was being aborted." Then
         Response.write(ex.message)
    End If

End Try
Echilon
  • 10,064
  • 33
  • 131
  • 217
-1

Its simple, you can try:

Response.Close(); in place of Response.End();

CDspace
  • 2,639
  • 18
  • 30
  • 36
Ankita_Shrivastava
  • 1,225
  • 11
  • 9