Link to home
Start Free TrialLog in
Avatar of calebbaity
calebbaity

asked on

Need to Modify C# Console App code to send output to CSV file.

I need to modify a C# console application to output a SQL result set to a CSV file. Currently the application sends the output to a Text box (textXMLResult).

Current Visual Studio Solution:
XXXApiSample
-Service References
-Properties
-References
-app.config
-C# ExampleCode.cs
-FormApi.cs
-C# Program.cs
-C# UseCase.cs
-C# Utility.cs


C# Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace XXXApiSample
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormAPI());
        }
    }
}

Open in new window



FormApi.cs
...
        private void buttonGetData_Click(object sender, EventArgs e)
        {
            this.textXMLResult.Text = "";
            ExampleCode ec = new ExampleCode();
            this.textXMLResult.Text = ec.GetData(this.textServiceUrl.Text,
                this.textUserName.Text,
                this.textPassword.Text,
                this.textScreenName.Text,
                this.textGetDataParams.Text);

        }
...

Open in new window



What I want to do is send the output of the query to a CSV file prior to the Textbox (textXMLResult).  In part because the textbox has a MaxLength of 32767 and also to do additional work on the file result set.
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

I am assuming your GetData method provides a CSV output, you can use the following code snippet.

StringBuilder sb = new StringBuilder();

sb = ec.GetData(this.textServiceUrl.Text,
                this.textUserName.Text,
                this.textPassword.Text,
                this.textScreenName.Text,
                this.textGetDataParams.Text);

File.WriteAllText(@"filename",sb.ToString());
 this.textXMLResult.Text = sb.ToString();

Open in new window

Avatar of calebbaity
calebbaity

ASKER

Actually, no.  This is new functionality that I am adding to the GetData method.  CSV is not provided.
So I would appreciate any guidance you can provide.

Also, with the code above, I am left with the impression that this code would get inserted between
lines #4 and #5 in the buttonGetDate_Click() function; is that correct?
Yupp. To generate a CSV I will need some sample data that your method returns from database.
I am working on getting a data dictionary. But, I don't have that yet.  
But for the sake of argument; lets day I have fields as follows:

tblProspects
Field1- ID
Field2- Name (String 50)
Field3-DOB (date)
Field4-Age (number)
Field5-Comments (Text 500 characters)

Query: Select * from tblProspects;

With Output to CVS file and Textbox.
ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India 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
Yes. Database connectivity has been verified.  Its just that the database is currently empty.
So I will need to create dummy data to verify.


However, I think you have given me everything I need.


Thank You!
Excellent Help. Even provided reference.  Thank You!!
You are welcome :)

Sorry was away for last few days :)