title: Server cannot append header after HTTP headers have been sent c# excel inline
body: <p>I have this problem for trying to return a excel inline response.</p>
  
  <p>I searched all through the help coding pages for answers to this and can not find something that works yet.</p>
  
  <p>I made a simplified example of what I am doing below. This issue always happen.</p>
  
  <p>Error in this line:</p>
  
  <pre><code>Line 56:         Response.AddHeader("content-disposition",
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>public partial class _Default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          PleaseWait();
          Call();
      }
  
      protected void Call()
      {
          strSql = "SELECT * FROM ....... ; ";
  
          SqlCommand cmd = new SqlCommand(strSql);
          DataTable dt = GetData(cmd);
  
          GridView GridView1 = new GridView();
          GridView1.AllowPaging = false;
          GridView1.DataSource = dt;
          GridView1.DataBind();
  
          Response.Clear();
          Response.Buffer = true;
          Response.BufferOutput = true;
          Response.ClearHeaders();
  
          Response.AddHeader("content-disposition",
           "attachment;filename= "Output.xls");
          Response.Charset = "";
          Response.ContentType = "application/vnd.ms-excel";
          StringWriter sw = new StringWriter();
          HtmlTextWriter hw = new HtmlTextWriter(sw);
  
          for (int i = 0; i &lt; GridView1.Rows.Count; i++)
          {
              GridView1.Rows[i].Attributes.Add("class", "textmode");
          }
          GridView1.RenderControl(hw);
  
          string style = @"&lt;style&gt; .textmode { mso-number-format:\@; } &lt;/style&gt;";
          Response.Write(style);
          Response.Output.Write(sw.ToString());
          Response.Flush();
          Response.End();
      }
  
      protected void PleaseWait()
      {
          this.Response.Write("&lt;meta http-equiv=X-UA-Compatible content=IE=8&gt;");
          this.Response.Write(@"&lt;style type=text/css media=all&gt;");
          this.Response.Write(@".loading");
          this.Response.Write(@"{");
          this.Response.Write(@"text-align: center;");
          this.Response.Write(@"padding-top: 30px;");
          this.Response.Write(@"border-width: 1px solid #000;");
          this.Response.Write(@"width: 300px;");
          this.Response.Write(@"height: 100px;");
          this.Response.Write(@"-ms-filter: alpha(opacity=90);");
          this.Response.Write(@"-ms-opacity: 0.90;");
          this.Response.Write(@"border-style: solid;");
          this.Response.Write(@"background-color: #FFFFFF;");
          this.Response.Write(@"position: absolute;");
          this.Response.Write(@"font-family: Trebuchet MS;");
          this.Response.Write(@"font-size: small;");
          this.Response.Write(@"position: absolute;");
          this.Response.Write(@"top: 0;");
          this.Response.Write(@"bottom: 0;");
          this.Response.Write(@"left: 0;");
          this.Response.Write(@"right: 0;");
          this.Response.Write(@"margin: auto;");
          this.Response.Write(@"display: block;");
          this.Response.Write(@"background: url('images/wait01.gif') no-repeat center;");
          this.Response.Write(@"}");
          this.Response.Write(@"&lt;/style&gt;");
  
          this.Response.Write(@"&lt;div id=mydiv class=loading&gt;&amp;nbsp;&lt;/div&gt;");
          this.Response.Write(@"&lt;script&gt;mydiv.innerText = '';");
          this.Response.Write(@"&lt;/script&gt;");
          this.Response.Write(@"&lt;script language=javascript&gt;;");
          this.Response.Write(@"var dots = 0;");
          this.Response.Write(@"var dotmax = 10;");
          this.Response.Write(@"function ShowWait()");
          this.Response.Write(@"{");
          this.Response.Write(@"var output;");
          this.Response.Write(@"output = 'Wait...';");
          this.Response.Write(@"dots++;");
          this.Response.Write(@"if(dots&gt;=dotmax)dots=1;");
          this.Response.Write(@"for(var x = 0;");
          this.Response.Write(@"x &lt; dots;x++)");
          this.Response.Write(@"{");
          this.Response.Write(@"output += '.';");
          this.Response.Write(@"}");
          this.Response.Write(@"mydiv.innerText =  output;");
          this.Response.Write(@"}");
          this.Response.Write(@"function StartShowWait()");
          this.Response.Write(@"{");
          this.Response.Write(@"mydiv.style.visibility = 'visible'; ");
          this.Response.Write(@"window.setInterval('ShowWait()',1500);");
          this.Response.Write(@"}");
          this.Response.Write(@"function HideWait()");
          this.Response.Write(@"{");
          this.Response.Write(@"mydiv.style.visibility = 'hidden';");
          this.Response.Write(@"window.clearInterval();");
          this.Response.Write(@"}");
          this.Response.Write(@"StartShowWait();");
          this.Response.Write(@"&lt;/script&gt;");
  
          this.Response.Flush();
          Thread.Sleep(500);
      }
  
      private DataTable GetData(SqlCommand cmd)
      {
          DataTable dt = new DataTable();
          String strConnString = System.Configuration.ConfigurationManager.
               ConnectionStrings["conn"].ConnectionString;
          SqlConnection con = new SqlConnection(strConnString);
          SqlDataAdapter sda = new SqlDataAdapter();
          cmd.CommandType = CommandType.Text;
          cmd.Connection = con;
          try
          {
              con.Open();
              sda.SelectCommand = cmd;
              sda.Fill(dt);
              return dt;
          }
          catch (Exception ex)
          {
              throw ex;
          }
          finally
          {
              con.Close();
              sda.Dispose();
              con.Dispose();
          }
      }
  
      public override void VerifyRenderingInServerForm(Control control)
      {
      }
  }   
  </code></pre>