//You should get data into datatable
DataTable dt=new DataTable;
// Write data in datatable to a text file
string path = Server.MapPath("")+"\\files\\temp.txt";
StreamWriter sw = new StreamWriter(path,false);
foreach(DataRow dr in dt.Rows)
{
string line="";
foreach(DataColumn dc in dt.Columns)
{
line+=dr[dc.Name].ToString()+"\t";
}
sw.WriteLine(line);
}
sw.Close();
//Read the file to a byte array
Stream s = File.OpenRead(path);
Byte[] buffer = new Byte[s.Length];
s.Read(buffer, 0, (Int32) s.Length);
//Clear the headers and write the byte arras as file to buffer
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.BinaryWrite(buffer);
Response.End();