title: Cannot append header after HTTP headers have been sent error not resolved
body: <p>This method is exporting the datatable into excel</p>
  
  <pre><code>  public static void ExportToExcel(DataTable table)
          {
              try
              {
                  HttpContext.Current.Response.Clear();
                  HttpContext.Current.Response.ClearContent();
                  HttpContext.Current.Response.ClearHeaders();
                  HttpContext.Current.Response.Buffer = true;
                  HttpContext.Current.Response.ContentType = "application/ms-excel";
                  HttpContext.Current.Response.Write(@"&lt;!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN""&gt;");
                  HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");
  
                  HttpContext.Current.Response.Charset = "utf-8";
                  HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
                  //sets font
                  HttpContext.Current.Response.Write("&lt;font style='font-size:10.0pt; font-family:Calibri;'&gt;");
                  HttpContext.Current.Response.Write("&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;");
                  //sets the table border, cell spacing, border color, font of the text, background, foreground, font height
                  HttpContext.Current.Response.Write("&lt;Table border='1' bgColor='#ffffff' " +
                                                     "borderColor='#000000' cellSpacing='0' cellPadding='0' " +
                                                     "style='font-size:11.0pt; font-family:Calibri; background:white;'&gt; &lt;TR&gt;");
                  //am getting my grid's column headers
                  int columnscount = table.Columns.Count;
  
                  for (int j = 0; j &lt; columnscount; j++)
                  {
                      //write in new column
                      HttpContext.Current.Response.Write("&lt;Td&gt;");
                      //Get column headers  and make it as bold in excel columns
                      HttpContext.Current.Response.Write("&lt;B&gt;");
                      HttpContext.Current.Response.Write(table.Columns[j].ToString());
                      HttpContext.Current.Response.Write("&lt;/B&gt;");
                      HttpContext.Current.Response.Write("&lt;/Td&gt;");
                  }
                  HttpContext.Current.Response.Write("&lt;/TR&gt;");
                  foreach (DataRow row in table.Rows)
                  {
  //write in new row
                      HttpContext.Current.Response.Write("&lt;TR&gt;");
                      for (int i = 0; i &lt; table.Columns.Count; i++)
                      {
                          HttpContext.Current.Response.Write("&lt;Td&gt;");
                          HttpContext.Current.Response.Write(HttpContext.Current.Server.HtmlEncode(row[i].ToString()));
                          HttpContext.Current.Response.Write("&lt;/Td&gt;");
                      }
  
                      HttpContext.Current.Response.Write("&lt;/TR&gt;");
                  }
                  HttpContext.Current.Response.Write("&lt;/Table&gt;");
                  HttpContext.Current.Response.Write("&lt;/font&gt;");
                  HttpContext.Current.Response.Flush();
                  HttpContext.Current.Response.End();
              }
              catch (Exception ex)
              {
                  string abc = ex.ToString();
              }
          }
  </code></pre>
  
  <p>This is my controller </p>
  
  <pre><code> public ActionResult ExportData()
          {
              var industryData = _rawDataHlper.GetIndustryData();
              if (industryData != null)
              {
                  ExportToExcel((industryData));
              }
              return RedirectToAction("Index");
          }
  </code></pre>
  
  <p>This method is getting records from the database</p>
  
  <pre><code> public DataTable GetIndustryData()
          {     
              IList&lt;v_IndustryDetail&gt; industryData = _insurancedb.v_IndustryDetail.OrderByDescending(x=&gt;x.ReportYear).ToList();
  
              DataTable dt = new DataTable();
  
              return dt;
  
          }
  </code></pre>
  
  <p>I am getting "cannot append header after HTTP headers have been sent" error. I want to redirect to Index action. I have tried searching this error and tried different solutions like instead of</p>
  
  <pre><code>Response.End()
  </code></pre>
  
  <p>Use</p>
  
  <pre><code>HttpContext.Response.SuppressContent = true;
  HttpContext.ApplicationInstance.CompleteRequest();
  </code></pre>
  
  <p>Also tried clearing the header</p>
  
  <pre><code>Response.ClearHeaders();
  </code></pre>
  
  <p>But no solution is working and I am getting the same error.</p>
  
  <p>Can anyone help me how to solve this. i have spend nearly a day to fix this but still struggling. Any help would be appreciated</p>
  
  <p>Thanks in advance</p>