Guys I searched in internet and found many solutions for exporting gridview to excel. I tried all the solutions but none of them worked for me. As suggested I traced the Application_Error module.
While debugging my execution goes to
//Global.asax
protected void Application_Error(object sender, EventArgs e)
{
Exception exc = Server.GetLastError();
}
The exception I am getting is :: Exception of type 'System.Web.HttpUnhandledException' was thrown.
I cannot figure out what am I doing wrong. Please can anyone suggest me, what is happening.
private void ExportGridToExcel()
{
try
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "PatientCount_" + DateTime.Now + ".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
GridView1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex) { }
}
While debugging I am not getting any exceptions but after the execution of this code, the execution goes to Global.asax file Application_Error module.
Please help me!!!