mugsey
asked on
Convert datareader to CSV and export to web browser in ASP.NET C#
I have a datareader
while (thisReader.Read())
{
}
Now how can I export the contents of this datarader to a web browser in CSV format, so the user can save the file on this desktop.
while (thisReader.Read())
{
}
Now how can I export the contents of this datarader to a web browser in CSV format, so the user can save the file on this desktop.
Try this:
private string ToCSV(DataTable dataTable)
{
DataTable schemaTable = myReader.GetSchemaTable();
StringBuilder sb = new StringBuilder();
foreach (DataColumn column in schemaTable.Columns)
{
sb.Append(column.ColumnName + ',');
}
sb.Append("\r\n");
foreach (DataRow row in schemaTable.Rows)
{
foreach (DataColumn column in schemaTable.Columns)
{
sb.Append(row[column].ToString() + ',');
}
sb.Append("\r\n");
}
return sb.ToString();
}
ASKER
OK thanks guys I will try in the next hour
ASKER
Thanks pratima_mcs
How can I specify the file name and where to save the file? Thanks
How can I specify the file name and where to save the file? Thanks
When you run this code it will ask with a popup to open , save
ASKER
OK thanks - so you know the line
Response.AddHeader("conten t-disposit ion", string.Format("attachment; filename={ 0}.csv", fileName));
Where does filename come from?
You see my code is like this
Currently I have a datareader that is populated from a dynamic search
while (thisReader.Read())
{
}
How can I amend your code above so it works
Response.AddHeader("conten
Where does filename come from?
You see my code is like this
Currently I have a datareader that is populated from a dynamic search
while (thisReader.Read())
{
}
How can I amend your code above so it works
Response.AddHeader("conten t-disposit ion", string.Format("attachment; filename={ 0}.csv", fileName));
in this they have used the parament send to the function ...
you can use your own tempory file name there like
Response.AddHeader("conten t-disposit ion", string.Format("attachment; filename=t emp.csv", fileName));
that will appear by default in save dialog
in this they have used the parament send to the function ...
you can use your own tempory file name there like
Response.AddHeader("conten
that will appear by default in save dialog
ASKER
OK thANKS
But could you amend your example so it wiould fit into the following?
while (thisReader.Read())
{
}
But could you amend your example so it wiould fit into the following?
while (thisReader.Read())
{
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window