The below snippet works fine to download a sql table to .csv however what does not work is that, when I try to give the file name dynamically like this change;
Instead of:
response.AddHeader("content-disposition", "attachment;filename=Logs.csv");
I use:
response.AddHeader("content-disposition", "attachment;filename="+sch.SchoolName+".csv");
It saves the file as .txt and not .csv why is that so?
Furthermore, to be crystal, I have a table which separates the records from eachother on the basis of the column MacNum. I am trying to generate two different .csv files from the same table but with different file names, If I uncomment the below part (commented one) it throws me a "Cannot send headers" exception.
public void ExportCSV_CloudLogs()
{
School sch = (from sc in db.Schools
where sc.MacNum == 1.ToString()
select sc).FirstOrDefault();
var sb = new StringBuilder();
var list = (from o in db.Logs where o.MacNum == 1
select o).ToList();
sb.AppendFormat("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}", "No", "Name", "Time", "Mode", "Ex", "Type", "SId", "Work", "sDate", "MachineNum", Environment.NewLine);
foreach (var item in list)
{
sb.AppendFormat("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}", item.No, item.Name, item.Time, item.Mode, item.Ex, item.Type, item.SId, item.Work, item.Date, item.MacNum, Environment.NewLine);
}
//Get Current Response
var response = System.Web.HttpContext.Current.Response;
response.BufferOutput = true;
response.Clear();
response.ClearHeaders();
response.ContentEncoding = Encoding.Unicode;
response.AddHeader("content-disposition", "attachment;filename="+sch.SchoolName+".csv");
response.ContentType = "text/plain";
response.Write(sb.ToString());
response.End();
//School sch2 = (from sc in db.Schools
// where sc.MacNum == 2.ToString()
// select sc).FirstOrDefault();
//var sb2 = new StringBuilder();
//var list2 = (from o in db.Logs
// where o.MacNum == 2
// select o).ToList();
//sb2.AppendFormat("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}", "No", "Name", "Time", "Mode", "Ex", "Type", "SId", "Work", "sDate", "MacNum", Environment.NewLine);
//foreach (var item in list2)
//{
// sb2.AppendFormat("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}", item.No, item.Name, item.Time, item.Mode, item.Exception, item.Type, item.SId, item.Work, item.Date, item.MacNum, Environment.NewLine);
//}
////Get Current Response
//var response2 = System.Web.HttpContext.Current.Response;
//response2.BufferOutput = true;
//response2.Clear();
//response2.ClearHeaders();
//response2.ContentEncoding = Encoding.Unicode;
//response2.AddHeader("content-disposition", "attachment;filename="+sch.SchoolName+".CSV ");
//response2.ContentType = "text/plain";
//response2.Write(sb2.ToString());
//response2.End();
}
UPDATE:
below is the picture of the fileName it is taking but when it only saves it with whs.
Furthermore, how do I run the same code again for another school in the same table? (the commented code in the above snippet)

var fileName = sch.SchoolName; response.AddHeader("content-disposition", "attachment;filename="+fileName+".csv");but this saves it as.txtagain. plus, instead of taking the full name as stored in db >ABC - O Levelsit only takesABCABC - O Levelsin db.response.AddHeader()work if the variablesch.SchoolNamehas the valueLogs(no spaces, no special chars, etc.). If that works than isn't your code the problem, but the data it contains.