0

I am new to programing and I would like to read a csv file into textboxes that I have on a form. Right now I am reading the file into a dataTable and was thinking I would then read it into the texboxes but I am not sure if I am going about this correct. Is there an easier way to do this? This is what I have so far:

protected void getftp()
{

   //create Data table to temporary storage
   var myTable = new DataTable();

   //add columns
   myTable.Columns.Add("Start_date");
   myTable.Columns.Add("End_date");
   //...snip...
   myTable.Columns.Add("Comments");

   //The 'using' command close connection when it is done
   using (var reader = new StreamReader(File.OpenRead(@"C:\ftp\inbox\test.csv")))
   {
      while (!reader.EndOfStream)
      {
         //read in one line of the file
         string line = reader.ReadLine();

         //create an array of strings from each value in the current line
         string[] values = line.Split(',');

         //add the array as a row in the DataTable
         myTable.Rows.Add(values);
      }
   }
}
SWeko
  • 30,434
  • 10
  • 71
  • 106
robert woods
  • 375
  • 2
  • 7
  • 20
  • It is generally a good idea to provide some code that indicates your approach, and be more specific as to what you are having problems with. In this way, you are helping the community here to help you. – Ioannis Karadimas Jan 24 '13 at 15:48
  • you can split the content of csv file, but this is not at all good approach – A.T. Jan 24 '13 at 15:48

2 Answers2

0

see the following link. It shows the basics of working with th CSV format http://www.codeproject.com/Articles/30705/C-CSV-Import-Export

Alex
  • 8,827
  • 3
  • 42
  • 58
0

Here is another SO Question regarding reading CSV Files in .NET:

Reading a CSV file in .NET?

This particular answer references 2 CSV Readers. You could use these to read in the CSV file and then set the values in your Textboxes on your Windows Form (or Web Form or ASPX or Razor page, you did not indicate your front-end).

I would recommend reusing one of these projects instead of re-inventing the wheel and rolling your own CSV parser. It's easy enough to write in C#, but reading/parsing CSV files is a problem that has been solved many times over.

Community
  • 1
  • 1
Philip Tenn
  • 6,003
  • 8
  • 48
  • 85