Link to home
Start Free TrialLog in
Avatar of mugsey
mugseyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

loop through datareader and export to csv file, use file dialog to save on users desktop

I have a datareader as follows

 while (thisReader.Read())
                {
                    strvalues += (string)thisReader["FirstName"].ToString() + ", " + (string)thisReader["LastName"].ToString() + ", ";
                    strvalues += (string)thisReader["Address1"].ToString() + ", " + (string)thisReader["PostCode"].ToString() + ", " + (string)thisReader["Country"].ToString() + ", ";


                    if (strvalues.Length > 2)
                        strvalues = strvalues.Substring(0, strvalues.Length - 2) + "\n";                                
                   
                }


Now how can I amend the above code so it produces a valid CSV or EXCEL file.  Also I need a dialog box that the user can select to save the file to the desk top
Avatar of SQL_SERVER_DBA
SQL_SERVER_DBA
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mugsey

ASKER

OK gang, thanks I will have a go tomorrow when I am back in work.  Thanks for your comments
Avatar of mugsey

ASKER

Actually I have a datareader not a gridview, can I do this with a datareader?
Yes, at the very top are 2 lines of code that load your reader into a DataTable:

            DataTable table = new DataTable();
            table.Load(thisReader);

            'Etc...

Avatar of mugsey

ASKER

VBRocks you know your stuff!!!!!!!!!!!!!!!!!!!!!!

Will update tomorrow if that is OK, however you will certainly get points.  Thanks again for your great comments
Avatar of mugsey

ASKER

Thanks but how can I do this

SaveFileDialog diag = new SaveFileDialog();

in ASP.NET

Is this not for windows forms?
You're right mugsey...  That is a windows form control.  And I"m not sure how to do that with ASP.NET.

My apologies.  :(



Avatar of mugsey

ASKER

No problem, I will award you the points for you contributions!
Can anyone append info on how to include the Headers in the CSV export?