Topic exported to excel?

I have a DataTable associated with a GridView. I also have a button that, when clicked, exports a DataTable to an Excel file. However, the following error occurs:

ErrMsg = "The thread breaks."

Here is the part of the code where the error occurs:

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 now it just goes to the finally block, and I cannot figure out which error message is being raised.

+4
source share
5 answers

The ThreadAbortException is thrown from the following line:

 HttpContext.Current.Response.End(); 

Here is more details and a workaround.

+9
source

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

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

Then, after hard work, I came across the code above. And it works great without any exception or unwanted code at the end of the downloaded file.

A source

+5
source

The exception was thrown by Response.End

One thing you can do is logically capture the exception message ...

this method will not affect the data, but it will catch only an 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 
0
source

I had a different story, I use telerik elements, and NONE helped me, in the end I found out that I just need to wrap my markup in telerik: RadAjaxPanel below is a code snippet to disable ajax for the event handler on the RequestStart Client side.

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

and then in my user control I added another function as shown below:

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

0
source

Simple, you can try:

Response.Close(); instead of Response.End();

-1
source

All Articles