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
mugseyAsked:
Who is Participating?
 
VBRocksCommented:
Here is how you can do it:

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

            SaveFileDialog diag = new SaveFileDialog();
            diag.DefaultExt = "csv";
            diag.Filter = "Comma Delimited Files (*.csv)|*.csv";
            DialogResult result = diag.ShowDialog();
            string delimiter = "";

            if (result == DialogResult.OK)
            {
                string path = diag.FileName;

                System.IO.StreamWriter writer = new System.IO.StreamWriter(path);
                foreach (DataRow row in table.Rows)
                {
                    delimiter = "";

                    foreach (DataColumn col in table.Columns)
                    {
                        writer.Write(delimiter + row[col].ToString());
                        delimiter = ",";
                    }
                    writer.Write(System.Environment.NewLine);
                   
                }

                writer.Close();
                writer.Dispose();
            }

0
 
SQL_SERVER_DBACommented:
0
 
mugseyAuthor Commented:
OK gang, thanks I will have a go tomorrow when I am back in work.  Thanks for your comments
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
mugseyAuthor Commented:
Actually I have a datareader not a gridview, can I do this with a datareader?
0
 
VBRocksCommented:
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...

0
 
mugseyAuthor Commented:
VBRocks you know your stuff!!!!!!!!!!!!!!!!!!!!!!

Will update tomorrow if that is OK, however you will certainly get points.  Thanks again for your great comments
0
 
mugseyAuthor Commented:
Thanks but how can I do this

SaveFileDialog diag = new SaveFileDialog();

in ASP.NET

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

My apologies.  :(



0
 
mugseyAuthor Commented:
No problem, I will award you the points for you contributions!
0
 
PhilTrackCommented:
Can anyone append info on how to include the Headers in the CSV export?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.