Solved

c# asp streamwriter append csv output

Posted on 2009-04-02
6
1,360 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name space syntax error 12 39
Remove greater than sign 3 42
ASP.NET Core index.html  LOCALHOST page cannot be found 2 22
SQL Login 17 38
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

930 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

14 Experts available now in Live!

Get 1:1 Help Now