tags:

views:

57

answers:

3

The following code writes the data and is working fine, but I want to add more than one client (maybe 10) in the .csv file. How can I achieve this. Thanks in advance.

private void createFileButton_Click(object sender, EventArgs e)
{
    string newFileName = "C:\\client_20100913.csv";

    string clientDetails = clientNameTextBox.Text + "," + mIDTextBox.Text + "," + billToTextBox.Text;

    //Header of the .csv File
    string clientHeader = "Client Name(ie. Billto_desc)" + "," + "Mid_id,billing number(ie billto_id)" + "," + "business unit id" + Environment.NewLine;

    File.WriteAllText(newFileName, clientHeader);
    File.AppendAllText(newFileName, clientDetails);

    MessageBox.Show("Client Added", "Added", MessageBoxButtons.OK); 
}
A: 

The underlying problem here seems to be where you're getting the data from to append to your CSV file. Your example code looks like it gets the various pieces of data from text boxes on the page, so if you want multiple clients, are they all going to have their data on the screen in text boxes? My instinct is probably not.

It sounds to me like you should be handling this client data using a class of some sort (perhaps persisted in a database) and then implement a method in the class called something like void AppendToCSV(string filename), which appends that client data to the CSV file. Then you can loop over your client objects, appending each one in turn.

How you produce/store your client objects, in relation to the text boxes you have on the screen, depends on what your app is trying to achieve.

NeilDurant
Yes I am getting the input from textboxes. The user want to add multiple clients to .csv file.
Ani
So I guess your app will follow some kind of CRUD pattern, so that the user can add clients one by one and they are added to some kind of container or database, and finally serialised out to CSV.
NeilDurant
+1  A: 

If you want to append the client information to an existing file, how about:

string newFileName = "C:\\client_20100913.csv";

string clientDetails = clientNameTextBox.Text + "," + mIDTextBox.Text + "," + billToTextBox.Text;


if (!File.Exists(newFileName))
{
    string clientHeader = "Client Name(ie. Billto_desc)" + "," + "Mid_id,billing number(ie billto_id)" + "," + "business unit id" + Environment.NewLine;

    File.WriteAllText(newFileName, clientHeader);
}

File.AppendAllText(newFileName, clientDetails);

This way the header line is only written the first time, when the file is created.

Although it would probably be even nicer to provide a list-detail view that lets you view all clients, add and remove clients, select a client to edit details, and save the complete file with all clients.

Jeremy
The user will enter client information from the textboxes. When one clinet is added it shd clear the textboxes and then user can enter another client information and then click add. After user clicks generate .csv that will generate file having two clints in there.
Ani
Once the file is created I want to keep adding text to it.
Ani
Is there any way where I can store inputs into array and then append the array to the file??
Ani
+1  A: 

It looks to me like you want a new client to be added every time you click the button.

If that's the case, the reason why it doesn't work currently is that the file is being cleared by the line

File.WriteAllText(newFileName, clientHeader);

The simplest change would be to check if the file exists before writing over it:

if (!File.Exists(newFileName))
{
    //Header of the .csv File
    string clientHeader = "Client Name(ie. Billto_desc)" + "," + "Mid_id,billing number(ie billto_id)" + "," + "business unit id" + Environment.NewLine;

    File.WriteAllText(newFileName, clientHeader);
}

Although you could use other strategies, such as creating the file on startup of the application and keeping it open (using something like a StreamWriter). You would then close the writer when your application exited. This would pretty much guarantee that the file couldn't be messed with while your application is open.

You might want to do this because there is a race condition in that code - after you check the file exists, and before you write to the file, a user could delete it. Keeping the file open helps to avoid this, but you may or may not want to do it.

Alex Humphrey
Is there any way where I can store inputs into array and then append the array to the file?? that way i can get 10 clients info. from the user and will then add it to the file
Ani
@Ani - yep, off the top of my head, if you store the clients in a `String[]` you can use `File.WriteAllLines`
Alex Humphrey
yes, even i was thinking the same, but I used the above approach and achieved it. Is there anyway to also put date to the name of the file.
Ani