Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I gotten this error " Server cannot append header after HTTP headers have been sent."

I Don't know where this is caused by in my code, anyone able to spot the error . Your help is appreciated, thank you !

C#
private void ExportExcel()
  {
      HttpContext.Current.Response.Clear();
      HttpContext.Current.Response.Buffer = true;
      HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
      HttpContext.Current.Response.Charset = "";
      HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
      using (StringWriter sw = new StringWriter())
      {
          HtmlTextWriter hw = new HtmlTextWriter(sw);
          //To Export all pages
          GridView1.AllowPaging = false;
          this.bindGridview1();
          GridView1.HeaderRow.BackColor = Color.White;
          foreach (TableCell cell in GridView1.HeaderRow.Cells)
          {
              cell.BackColor = GridView1.HeaderStyle.BackColor;
          }
          foreach (GridViewRow row in GridView1.Rows)
          {
              row.BackColor = Color.White;
              foreach (TableCell cell in row.Cells)
              {
                  if (row.RowIndex % 2 == 0)
                  {
                      cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                  }
                  else
                  {
                      cell.BackColor = GridView1.RowStyle.BackColor;
                  }
                  cell.CssClass = "textmode";
              }
          }
          GridView1.RenderControl(hw);
          //style to format numbers to string
          string style = @"<style> .textmode { mso-number-format:\@; } </style>";
          HttpContext.Current.Response.Write(style);
          HttpContext.Current.Response.Output.Write(sw.ToString());
          HttpContext.Current.Response.Flush();
          HttpContext.Current.Response.SuppressContent = true;
          HttpContext.Current.ApplicationInstance.CompleteRequest();
      }

  }

C#
protected void btnDel_Click(object sender, EventArgs e)
    {
        try
        {
            PanelPopup2.Visible = true;
            while (PanelPopup2.Visible == true)
            ExportExcel();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
Posted

1 solution

Remove the SuppressContent line and ensure the code doesn't go on to do any Redirects or Writes.
 
Share this answer
 

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