DotNet By Ajay — New: ES2025 cheat-sheet is live!
Showing posts with label Unhandled Exception. Show all posts
Showing posts with label Unhandled Exception. Show all posts

February 7, 2017

System.Threading.ThreadAbortException: Thread was being aborted.

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

ErrorMsg = "Thread was being aborted."

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


    private void ExportToExcel()
    {
        try
        {
            DataTable dt = (DataTable)ViewState["dtSearchPropertyData"];
            if (dt != null && dt.Rows.Count > 0)
            {
                GridView _gv = new GridView();
                _gv.DataSource = dt;
                _gv.DataBind();
                string attachment = "attachment; filename=Data.xls";
                Response.Clear();
                Response.ClearContent();
                Response.AddHeader("content-disposition", attachment);
                Response.ContentType = "application/vnd.ms-excel";
                using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    System.Web.UI.HtmlTextWriter htmltextwriter = new System.Web.UI.HtmlTextWriter(sw);
                    _gv.AllowPaging = false;

                    _gv.RenderControl(htmltextwriter);
                    Response.Output.Write(sw.ToString());
                    Response.Flush();
                    Response.End();
                }
            }
        }
        catch (Exception ex)
        {
            throw(ex);
        }
    }

Here The ThreadAbortException is thrown from the following line:

                    Response.End();

To resolve this :

/*Replace code */
                    Response.Flush();
                    Response.End();


/*With below line*/

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

Now this is working fine.

November 10, 2016

Unable to find control id on panel tab change.

Unable to find control id 'txtDOJ' referenced by the 'ControlToCompare' property of 'cvtxtEffectiveFrom'.

Description: An unhanded exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Unable to find control id 'txtDOJ' referenced by the 'ControlToCompare' property of 'cvtxtEffectiveFrom'.


Source Error:


An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


I am phasing this problem during accessing control id from Ajax TabContainer. Here I create multiple tab, and i want to access data from first tab to another tab. But this is throwing exception of "Unable to find control id."