Click here to Skip to main content
15,066,496 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

In my project I am exporting excel file to view some data's. When I am clicking the button it throws an error as follows(The error is thrown in the line Response.End):

[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

Here is the Code:

public static void ExportDataTableToCSV(DataTable dataTable, string CSVFileName)
    {
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        System.Web.HttpContext context = HttpContext.Current;
        context.Response.Clear();
        context.Response.ContentType = "text/csv";
        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + CSVFileName + ".csv");
        //Write a row for column names
        foreach (DataColumn dataColumn in dataTable.Columns)
            context.Response.Write(dataColumn.ColumnName + ",");
        context.Response.Write(Environment.NewLine);
        //Write one row for each DataRow
        foreach (DataRow dataRow in dataTable.Rows)
        {
            for (int dataColumnCount = 0; dataColumnCount < dataTable.Columns.Count; dataColumnCount++)
                context.Response.Write(dataRow[dataColumnCount].ToString() + ",");
            context.Response.Write(Environment.NewLine);
        }
        response.End();   //Here I am getting the error   


}

Can anyone help me to solve this issue.

Thanks in Advance
Posted

1 solution

I am quoting the cause and resolution from Microsoft Support[^] site -

CAUSE:
The Response.End method ends the page execution and shifts the execution to the Application_EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed.

This problem occurs in the Response.Redirect and Server.Transfer methods because both methods call Response.End internally. 

RESOLUTION:
To work around this problem, use one of the following methods:

    For Response.End, call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_EndRequest event.
    For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that passes false for the endResponse parameter to suppress the internal call to Response.End. For example:

      Response.Redirect ("nextpage.aspx", false);
    						
    If you use this workaround, the code that follows Response.Redirect is executed.
    For Server.Transfer, use the Server.Execute method instead.


By the way, I found this USEFUL link by Googling for your error message. If you need more discussion you can always use the Google search link[^]

Hope this helps!
   
Comments
Ankur\m/ 26-Jul-11 7:24am
   
Note: Just thought users won't have to navigate to another link if they have arrived here, and so copy-pasted the important part. :)
Member 7946563 26-Jul-11 7:27am
   
I used the solution HttpContext.Current.ApplicationInstance.CompleteRequest() it works fine but when opening the excel it displays some unwanted content inside rge excel file
Ankur\m/ 26-Jul-11 8:22am
   
Do you have any code after this line?
Member 7946563 26-Jul-11 8:25am
   
No
Ankur\m/ 26-Jul-11 8:36am
   
Response.End() was doing an abrupt end of the response so any code after that wouldn't have executed.
Now that you are using HttpContext.Current.ApplicationInstance.CompleteRequest() the response will be completed.
I guess you are calling this function from somewhere. Is there any code after this is called? You also need to check Application_EndRequest method in your Global.asax file. There must be some code. Probably these lines of code is causing these unwanted characters.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900