Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i got this code to insert text into a csv file:

C#
try
               {

                   for (int i = 0; i < this.TestCases; i++)
                   {


                       textClientNumber[i] = readClientNumber.ReadLine();
                       textCardNumber[i] = readCardNumber.ReadLine();

                       export.AddRow();
                       export["TransactionDate"]= DateTime.Now.ToString("yyyyMMdd");
                       export["Card Type"] = "MC";
                       export["Original Payment Reference"] = " ";
                       export["Retreival Reference"] = " ";
                       export["Credit/Debit Card Indicator"] = " ";
                       export["Institution_number"] = this.Institution_number;
                       export["Refund"] = cmbRefund.SelectedItem.ToString();
                       export["Card Number"] = textCardNumber[i].ToString();
                       export["Capture Method"] = cmbCaptureMethod.SelectedValue.ToString();
                       export["ECI Indicator / Rate Program"] = cmbEciIndicator.SelectedItem.ToString();
                       export["Client Number"] = textClientNumber[i].ToString();
                       export["Processor ID"] = " ";
                       export["Currency"] = cmbCurrency.SelectedValue.ToString();
                       int randnum1 = rand.Next(1, 999999);
                       string randnumstring = opClass.AmountToExport(randnum1);
                       export["Amount"] =randnumstring ;
                   }


                   export.ExportToFile("C:\\temp\\MCB2" + randnum + ".csv");
                   this.Excelfilename = "MCB2" + randnum;
                   MessageBox.Show("Csv File created, You can find the file in C:\\temp\\" + this.Excelfilename);

               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.ToString());
               }



Can someoent ell me how can i begin add rows from the last used row or maybe another technique how to do it ?

What I have tried:

I tried this code to append rows to the exisiting .csv file:

C#
  try
                {
                    StreamReader reader = File.OpenText(this.Excelfilename + ".csv");
                    
                }
                catch (Exception Ex)
                {
                    Console.WriteLine(Ex.ToString());


                var firstLetterCharacters = this.Excelfilename.TakeWhile(Char.IsLetter);
                string filename = new string(firstLetterCharacters.ToArray());
                }




if (filename == "MCB")
            {
                try
                {
                        

                        for (int i = 0; i < this.TestCases; i++)
                        {

                            textClientNumber[i] = readClientNumber.ReadLine();
                            textCardNumber[i] = readCardNumber.ReadLine();

                            export.AddRow();

                            export["TransactionDate"] = DateTime.Now.ToString("yyyyMMdd");
                            export["Card Type"] = "MC";
                            export["Original Payment Reference"] = " ";
                            export["Retreival Reference"] = " ";
                            export["Credit/Debit Card Indicator"] = " ";
                            export["Institution_number"] = this.Institution_number;
                            export["Refund"] = cmbRefund.SelectedItem.ToString();
                            export["Card Number"] = textCardNumber[i].ToString();
                            export["Capture Method"] = cmbCaptureMethod.SelectedValue.ToString();
                            export["ECI Indicator / Rate Program"] = cmbEciIndicator.SelectedItem.ToString();
                            export["Client Number"] = textClientNumber[i].ToString();
                            export["Processor ID"] = " ";
                            export["Currency"] = cmbCurrency.SelectedValue.ToString();
                            int randnum1 = rand.Next(1, 999999);
                            string randnumstring = opClass.AmountToExport(randnum1);
                            export["Amount"] = randnumstring;

                            

                        }
                    if (File.Exists("C:\\temp\\" + this.Excelfilename + ".csv"))
                    {
                        File.Delete(this.Excelfilename);
                        export.ExportToFile("C:\\temp\\" + this.Excelfilename + ".csv");
                    }



everything runs perfectly by the old data will be lost and be replaced to the newest data.
Posted
Updated 29-Aug-17 23:18pm

1 solution

You have two methods: you can append the text, using File.AppendText Method (String) (System.IO)[^] or by using a stream with the Append option:
C#
using (FileStream fs = new FileStream(fileName,FileMode.Append, FileAccess.Write))
    {
    using (StreamWriter sw = new StreamWriter(fs))
        {
        sw.WriteLine(myNewCSVLine);
        }
    }

The only other way is to read the entire file (using a string collection, or a CSVReader) append your data, and write it all back to a new file.
 
Share this answer
 
Comments
Joe Doe234 30-Aug-17 5:23am    
Hi OriginalGriff, Thank for helping.

One small question, how can i save "MyNewCsvLine" because i have more than 1 record in that line .

Regards,
Sigmond
OriginalGriff 30-Aug-17 5:34am    
Separate them with "\n", or write each line separately in a loop if you have them in a collection.

Or, there is string.Join ... loads of ways, depending on how you have the info stored - which I don't know!
Joe Doe234 30-Aug-17 5:39am    
OK thank you :)
OriginalGriff 30-Aug-17 5:42am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900