Solved

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

Posted on 2007-11-21
10
3,364 Views
Last Modified: 2008-05-08
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
0
Comment
Question by:mugsey
10 Comments
 
LVL 16

Expert Comment

by:SQL_SERVER_DBA
ID: 20329145
0
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 20329390
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
 

Author Comment

by:mugsey
ID: 20330483
OK gang, thanks I will have a go tomorrow when I am back in work.  Thanks for your comments
0
 

Author Comment

by:mugsey
ID: 20330867
Actually I have a datareader not a gridview, can I do this with a datareader?
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20330886
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:mugsey
ID: 20330918
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
 

Author Comment

by:mugsey
ID: 20333593
Thanks but how can I do this

SaveFileDialog diag = new SaveFileDialog();

in ASP.NET

Is this not for windows forms?
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20350490
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
 

Author Comment

by:mugsey
ID: 20364618
No problem, I will award you the points for you contributions!
0
 

Expert Comment

by:PhilTrack
ID: 31793849
Can anyone append info on how to include the Headers in the CSV export?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now