I am trying to convert datatable to csv format.The code is working and the getting the csv correctly.But the problem is there is some exception is showing the catch block after Response.End();
here is my code
public void GetCSV(DataTable dt)
{
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames1 = dt.Columns.Cast<DataColumn>().
Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames1));
foreach (DataRow row in dt.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
foreach (var item in fields)
{
sb.AppendFormat("\"{0}\",", item.Replace("\"", "\"\""));
}
sb = sb.Remove(sb.Length - 1, 1);
sb.Append("\n");
//sb.AppendLine(string.Join(",", fields));
}
string attachment1 = "attachment; filename=Application_Data.csv";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment1);
Response.ContentType = "application/csv;charset=utf-8";
Response.Charset = "utf-8";
Response.Write(sb.ToString());
Response.End();
}
I just debug and check .then the Response.End having the issue
the error showing is
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack
Can any one know what is the issue.Thanks in advance for help.