Solved

c# asp streamwriter append csv output

Posted on 2009-04-02
6
1,357 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
  • 3
  • 2
6 Comments
 
LVL 18

Assisted Solution

by:ChetOS82
ChetOS82 earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:Bonnie_K
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now