Avoiding Response.End () "Subject Aborts" Exception While Loading Excel File

I tried to convert my dataset to excel and load that excel. I got the required excel file. But a System.Threading.ThreadAbortException was raised every time you load excel. How to solve this problem? .. Please help me ...

I call this method in my aspx screen. The same exception caused this method.

I call the public void function ExportDataSet (DataSet ds) in many aspx screens, and I also support the error log method for exceptions that occur at run time, and these exceptions are written to TXT files. Thus, the same exception is recorded in all txt aspx screen files. I just want this exception to select the aspx file class from the declared class. I just just want to handle this exception in the method declaration class file itself.

call the ASPX file method: excel.ExportDataSet (dsExcel);

Method Definition:

public void ExportDataSet(DataSet ds) { try { string filename = "ExcelFile.xls"; HttpResponse response = HttpContext.Current.Response; response.Clear(); response.Charset = ""; response.ContentType = "application/vnd.ms-excel"; response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\""); using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { GridView dg = new GridView(); dg.DataSource = ds.Tables[0]; dg.DataBind(); dg.RenderControl(htw); // response.Write(style); response.Write(sw.ToString()); response.End(); // Exception was Raised at here } } } catch (Exception ex) { string Err = ex.Message.ToString(); EsHelper.EsADLogger("HOQCMgmt.aspx ibtnExcelAll_Click()", ex.Message.ToString()); } finally { } } 
+85
Jan 08 '14 at 6:27
source share
16 answers

I researched online and saw that Response.End() always throws an exception.

Replace this: HttpContext.Current.Response.End();

With this:

 HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client. HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client. HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event. 
+157
Mar 12 '14 at 20:44
source share

This helped me handle the exception Thread was being aborted ,

 try { //Write HTTP output HttpContext.Current.Response.Write(Data); } catch (Exception exc) {} finally { try { //stop processing the script and return the current result HttpContext.Current.Response.End(); } catch (Exception ex) {} finally { //Sends the response buffer HttpContext.Current.Response.Flush(); // Prevents any other content from being sent to the browser HttpContext.Current.Response.SuppressContent = true; //Directs the thread to finish, bypassing additional processing HttpContext.Current.ApplicationInstance.CompleteRequest(); //Suspends the current thread Thread.Sleep(1); } } 

if you use the following code instead of HttpContext.Current.Response.End() , you will get a Server cannot append header after HTTP headers have been sent exception Server cannot append header after HTTP headers have been sent .

  HttpContext.Current.Response.Flush(); HttpContext.Current.Response.SuppressContent = True; HttpContext.Current.ApplicationInstance.CompleteRequest(); 

Hope this helps

+10
Sep 10 '15 at 15:29
source share

The same question arises as:

When ASP.NET System.Web.HttpResponse.End () is called, is the current thread interrupted?

So it is by design. You need to add catch for this exception and gracefully β€œignore” it.

+4
Jan 08 '14 at 6:32
source share

Use a special catch block to exclude the Response.End () method

 { ... context.Response.End(); //always throws an exception } catch (ThreadAbortException e) { //this is special for the Response.end exception } catch (Exception e) { context.Response.ContentType = "text/plain"; context.Response.Write(e.Message); } 

Or just remove Response.End () if you create a file manipulator

+3
Mar 14 '17 at 13:03 on
source share

Move Response.End () to the outside of the Try / Catch and Using blocks.

Suppose you threw an exception to get around the rest of the request, you simply could not catch it.

 bool endRequest = false; try { .. do stuff endRequest = true; } catch {} if (endRequest) Resonse.End(); 
+2
Jul 10 '15 at 18:22
source share

Only works for me

HttpContext.Current.ApplicationInstance.CompleteRequest ().

stack overflow

+2
Aug 17 '17 at 3:06 on
source share

Just put

 Response.End(); 

in the finally block , not in the try block.

It worked for me !!!.

I had the following problematic code structure (with exception)

 ... Response.Clear(); ... ... try{ if (something){ Reponse.Write(...); Response.End(); return; } some_more_code... Reponse.Write(...); Response.End(); } catch(Exception){ } finally{} 

and this throws an exception. I suspect that an Exception is thrown where there is code / work to execute after response.End () ;, In my case, the extra code was just the result.

When I just moved response.End (); to the finally block (and left the return in its place), which leads to skipping the rest of the code in the try block and jumping to the finally block (not just leaving the contained function)), the Exception stopped happening.

The following steps work fine:

 ... Response.Clear(); ... ... try{ if (something){ Reponse.Write(...); return; } some_more_code... Reponse.Write(...); } catch(Exception){ } finally{ Response.End(); } 
+1
Feb 19 '17 at 17:30
source share

error for Response.END (); because you are using asp upgrade toolbar or any control that uses javascript try using your own control from asp or html without javascript or scriptmanager or scripts and try again

+1
Aug 24 '17 at 17:01
source share

I removed the link from UpdatePanel and also commented on Response.End () Success !!!

+1
Sep 22 '17 at 16:38 on
source share

This is not a problem, but it is by design. The main reason is described on the Microsoft support page.

The Response.End method completes the page and transfers the execution to the Application_EndRequest event in the application event pipeline. The line of code that follows Response.End is not executed.

Provided solution:

For Response.End, call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass code execution in the Application_EndRequest event

Here is the link: https://support.microsoft.com/en-us/help/312629/prb-threadabortexception-occurs-if-you-use-response-end--response-redi

+1
Nov 10 '17 at 6:15
source share

I recommend this solution:

  1. Do not use response.End();

  2. Declare this global var: bool isFileDownLoad;

  3. Right after your (response.Write(sw.ToString());) set ==> isFileDownLoad = true;

  4. Redefine your render as:

     /// AEG : Very important to handle the thread aborted exception override protected void Render(HtmlTextWriter w) { if (!isFileDownLoad) base.Render(w); } 
0
Jan 16 '14 at 15:10
source share

discard the response to the client before the answer .end ()

Read more about Response.Flush Method

So use the code below response.End();

 response.Flush(); 
0
Feb 17 '17 at 8:09
source share

I used all of the above changes, but still I had the same problem in my web application.

Then I contacted my hosting and asked them to check if any software or antivirus is forbidden to block our files via HTTP. or ISP / network does not allow file transfer.

They checked the server settings and bypassed the "General data center firewall" for my server, and now our application can download the file.

Hope this answer helps someone. This is what worked for me

0
Nov 13 '17 at 4:14
source share

I have found a reason. If you remove the update panels, it works great!

0
Oct 25 '18 at 10:11
source share

Right. This works well for me. thank you in advance

0
May 9 '19 at 10:47
source share

I found that the following works better ...

  private void EndResponse() { try { Context.Response.End(); } catch (System.Threading.ThreadAbortException err) { System.Threading.Thread.ResetAbort(); } catch (Exception err) { } } 
0
May 14 '19 at
source share



All Articles