[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1390
  • Last Modified:

c# asp streamwriter append csv output

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
Bonnie_K
Asked:
Bonnie_K
  • 3
  • 2
2 Solutions
 
ChetOS82Commented:
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
 
Craig WagnerSoftware ArchitectCommented:
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
 
Craig WagnerSoftware ArchitectCommented:
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
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.

 
Bonnie_KAuthor Commented:
I changed gw.WriteLine(filecontents); to gw.Write(filecontents); and the extra lines are gone.  Thanks a lot.
0
 
Bonnie_KAuthor Commented:
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
 
Bonnie_KAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now