Click here to Skip to main content
14,934,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Guys,

I am writing an assignment to save some data that the user enters into a CSV file. Every time I run the program I seem to loose the data that was previously saved. If you could advise me on what route I should take.

using System;
using System.IO;
using System.Collections.Generic;

namespace FileAccess
    class MainClass
        public static void Main(string[] args)
            string FName;
            string LName;
            string Dpt;
            string Grade;
            string NumberOfHours;
            string file_name = "EmployeeDetails.txt";
            string tLine = string.Empty;
            System.IO.StreamWriter objWriter;
            objWriter = new System.IO.StreamWriter(file_name);
            String[] ArrayDetails = new string[5];
            Console.Write("Enter Emp First Name: ");
            Console.Write("Enter Emp Last Name: ");
            LName = Console.ReadLine();
            Console.Write("Enter Emp Department: ");
            Dpt = Console.ReadLine();
            Console.Write("Enter Employee Grade: ");
            Grade = Console.ReadLine();
            Console.Write("Enter Number Of Hours Worked: ");
            NumberOfHours = Console.ReadLine();
            ArrayDetails[0] = FName;
            ArrayDetails[1] = LName;
            ArrayDetails[2] = Dpt;
            ArrayDetails[3] = Grade;
            ArrayDetails[4] = NumberOfHours;
            List<string> LDetails = new List<string>(); // Create new list of strings
            LDetails.Add(FName); // Add string 1
            LDetails.Add(LName); // 2
            LDetails.Add(Dpt); // 3
            LDetails.Add(Grade); // 4
            LDetails.Add(NumberOfHours); // 5
            string LDetailsCSV = string.Join(",", LDetails.ToArray());
Updated 14-Apr-11 17:48pm

1 solution

Did you mean CSV (Comma-separated Values)?

There is nothing in this code which should fail, but it does not look like you're trying to write a CSV file, it writes a single CSV line. The code is pretty bad. You hard-code everything using immediate constants. Take the constant of 5. What is there are more components? Why array of 5 values + 5 separate named variables. Do not Repeat Yourself (D.R.Y.). You read the variable and later put values in the array anyway. Instead of "" use string.Empty. Close file in the finally section of try-catch-finally block. Never use "\r\n", use portable System.Environment.NewLine.
Even better, use correct combination or Write and WriteLine. You console input is ugly due to using only WriteLine

As I understand from your comment, you wanted to append new data to the same file.
In this case, you should add boolean parameter append to the constructor of StreamWriter:[^].
(This is kind of bad; it you are a beginner, read help before using an existing class.
Please don't get offended, improve your approach, which should help you.)

fazleh ahmed 14-Apr-11 20:51pm
Forgive my code. I am just a beginner. I am trying to write a Comma-separated Values File. Once the employee information is added it should be saved into the file. When new data is enter it should enter below it. However, I am failing to do that. It seems to replace it each time.

Thanks for the quick reply.
Sergey Alexandrovich Kryukov 14-Apr-11 21:47pm
You're welcome. The code cannot be blamed or forgiven. Fix it. Beginner or not. If you're a biginner -- do begin. What do you mean "to replace each time". Do you mean next run-time? Your code is replacing be definition, because you create a new file each time. Do you want to append to the same file?!
You should have told exactly this word then.
fazleh ahmed 14-Apr-11 22:12pm
Yes that is what I meant.
1. I enter the first 5 data. (which my code so far saves onto file).
2. In the next run time i want to enter another 5 sets of data which should be put in the next LINE (underneath the previous one).

I hope thats clear enough for you. "Every time I run the program I seem to loose the data that was previously saved." I thought that meant the next run time?
Sergey Alexandrovich Kryukov 14-Apr-11 22:34pm
In this case, you problem is solved. See my updated Answer, after [EDIT].
Pong D. Panda 14-Apr-11 22:23pm
Well said! My 5!
Sergey Alexandrovich Kryukov 14-Apr-11 22:35pm
Thank you, Pong.
fazleh ahmed 14-Apr-11 22:49pm
Its fine I managed to solve it.
<pre lang="midl">objWriter.Write(LDetailsCSV);</pre>

is supposed to be
<pre lang="midl">objWriter.WriteLine(LDetailsCSV);</pre>

Didn't need to read help. Just asked the the same question somewhere else and got an answer straight away. Looks like there are other people out there, who use the same approach.
Sergey Alexandrovich Kryukov 14-Apr-11 23:21pm
You did not get any answer. You problem is not solved.
My update in my answers tells you how to do this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900