Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-21
10
Medium Priority
?
3,375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 16

Expert Comment

by:SQL_SERVER_DBA
ID: 20329145
0
 
LVL 27

Accepted Solution

by:
VBRocks earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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