title: Download file stored in SQL database from C#
body: <p>I have SQL database where I am storing user uploaded file.</p>
  
  <p>I have created a <code>linkbutton</code> to download the file. On click of that link button I am calling below code. Unfortunately it is not working. Not event throwing any error.</p>
  
  <p>I have stored <strong>filename</strong>, <strong>contentType</strong> and <strong>bytes</strong> as 3 columns in my SQL table</p>
  
  <pre><code>Response.Clear();
  Response.Buffer = true;
  Response.Charset = "";
  Response.Cache.SetCacheability(HttpCacheability.NoCache);
  Response.ContentType = contentType;
  Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
  Response.BinaryWrite(bytes);
  Response.Flush();
  Response.End();
  </code></pre>
  
  <p>What could be the issue? Or Any other way to achieve this?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>With same code and changes based on answer marked as correct, I did changes and it worked. Providing working code here for future reference of other users:</p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
              DataTable dt = Session["fileAttachment"] as DataTable;
              string fileName = Convert.ToString(dt.Rows[0]["filename"]);
              string contentType = Convert.ToString(dt.Rows[0]["contentType"]);
              byte[] bytes = Convert.FromBase64String(Convert.ToString(dt.Rows[0]["bytearr"]));
              Response.Clear();
              Response.ClearHeaders();
              Response.Buffer = true;
              Response.Charset = "";
              Response.Cache.SetCacheability(HttpCacheability.NoCache);
              Response.ContentType = contentType;
              Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
              Response.BinaryWrite(bytes); //
              //Response.BufferOutput = true;
              //Response.OutputStream.Write(bytes, 0, bytes.Length);
              Response.Flush();
              Response.End();
      }
  </code></pre>
  
  <p>Added above code in new page and redirected to this page after created session</p>