Solved

c# asp streamwriter append csv output

Posted on 2009-04-02
6
1,366 Views
Last Modified: 2012-05-06
Hi,

I have a form that is a questionnaire.  I am trying to collect the answers into a csv form.  One line per submit.

I am using the streamwriter.  The only way I could figure out how to do this was to open the file, copy the contents, write them to the new file then add to it. I could not figure out how to append to the file any other way.  

Every time the form is submitted, a blank line is added.

Here are the results of submitting the form 5 times, there is a blank line between each line of actual data:


/wEPDwUKLTQ2MDIzNTYwMmRkPlZhgR1uqeRKobS+TutZOKNgPeM=,one,two,Submit

/wEPDwUKLTQ2MDIzNTYwMmRkPlZhgR1uqeRKobS+TutZOKNgPeM=,three,four,Submit

/wEPDwUKLTQ2MDIzNTYwMmRkPlZhgR1uqeRKobS+TutZOKNgPeM=,five,six,Submit

/wEPDwUKLTQ2MDIzNTYwMmRkPlZhgR1uqeRKobS+TutZOKNgPeM=,seven,eight,Submit

/wEPDwUKLTQ2MDIzNTYwMmRkPlZhgR1uqeRKobS+TutZOKNgPeM=,nine,ten,Submit



Can I somehow just append the file instead of having to copy the contents first?

Can I somehow delete the blank lines?

Thanks,
Bonnie
protected void Button1_Click(object sender, EventArgs e)
    {
 
        String outputFile = Server.MapPath("./results.csv");
        StreamReader sr = new StreamReader(inputFile);
        String filecontents = sr.ReadToEnd();
        sr.Close();
 
        StreamWriter gw = new StreamWriter(outputFile, false);
        string answers = "";
 
        gw.WriteLine(filecontents);
 
        foreach (string item in Request.Form)
        {
           
            if (Request.Form[item] != null)
            {
                if (answers.Length == 0)
                {
                    answers = Request.Form[item];
                }
                else 
                {
                    answers = answers + "," + Request.Form[item];
                }
                
            }
        }
        gw.WriteLine(answers);
        gw.Close();
    }
}

Open in new window

0
Comment
Question by:Bonnie_K
[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
  • 3
  • 2
6 Comments
 
LVL 18

Assisted Solution

by:ChetOS82
ChetOS82 earned 150 total points
ID: 24048848
I would forget the StreamWriter and just use the System.IO.File.AppendText method.  You pass a file name and the string you want to append to the file.
0
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 350 total points
ID: 24054437
StreamWriter has an overload the takes a path and a boolean indicating whether you want to overwrite or not.

using( StreamWriter writer = new StreamWriter( myFilePath, true ) )
{
    writer.WriteLine( someStuffToWrite );
}

My guess is that blank lines are an artifact of your copying operation. You're probably using a WriteLine where you should be using a Write somewhere. I think it's a red herring that you shouldn't spend any time on until you resolve the first issue.
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 24054498
P.S. Here's a link to the MSDN info for that constructor...

http://msdn.microsoft.com/en-us/library/36b035cb.aspx

AppendText only opens the file for Append and returns a StreamWriter. AppendAllText would open the file, append the text and close the file.

So you've got three choices on how to proceed, depending on what your specific needs are.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:Bonnie_K
ID: 24054660
I changed gw.WriteLine(filecontents); to gw.Write(filecontents); and the extra lines are gone.  Thanks a lot.
0
 

Author Closing Comment

by:Bonnie_K
ID: 31565733
I am splitting the points because I did not get a chance to test the append method, am guessing it would work and I appreciate the contribution.  Hope that's OK.
0
 

Author Comment

by:Bonnie_K
ID: 24054801
Changing the streamwriter to:   StreamWriter gw = new StreamWriter(outputFile,true); and getting rid of the streamreader \ copy  also performed an append function
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

696 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