Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c# asp streamwriter append csv output

Posted on 2009-04-02
6
Medium Priority
?
1,384 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 600 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 1400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

636 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