2

We read a tab delimited file into a DataTable with the following code.

//read the uploaded file...
    var records = File.ReadAllLines(Server.MapPath("~/UploadFiles/") + Session.SessionID + "/orderEDI.txt").ToList();
    //load the data into the temporary table...
    records.ForEach(record => loadTable.Rows.Add(record.Split((char)9)));

This works just fine providing there are not more tabs in the file than there are columns in the DataTable.

I'm looking to find out if there's a way I can limit the number of columns it reads from the file. Or any other suggestions around this issue. It must read an absolute minimum of 10 columns (and ideally, only 10)

I build the DataTable and add columns to it before this load occurs. Would it be better to not add columns and just load the file into the DataTable and then read the table by column index rather than name?

Really not sure which way to go and would appreciate some experienced opinions.

Thanks

Stuart
  • 1,544
  • 5
  • 29
  • 45
  • You could try looking here, which seems to cover this query nicely: http://stackoverflow.com/questions/1050112/how-to-read-a-csv-file-into-a-net-datatable – Martin Mar 20 '13 at 13:51

1 Answers1

0

Since split results in an array, why don't you just use Take(10)?

//read the uploaded file...
    var records = File.ReadAllLines(Server.MapPath("~/UploadFiles/") + Session.SessionID + "/orderEDI.txt").ToList();
    //load the data into the temporary table...
    records.ForEach(record => loadTable.Rows.Add((record.Split((char)9)).Take(10)));
Mike Cofoed
  • 151
  • 1
  • 4
  • sounds good... but when I tried this I get "System.Linq.Enumerable+d__3a1[System.String]" in the first column of the DataTable – Stuart Mar 20 '13 at 15:33