0

I'm trying to read the following csv file:

Column1,Column2,Column3
MyData1,MyData2,MyData3
MyData11,MyData22,MyData33

Before I had the file stored locally and I would read it into my object like this:

string path = myPath;

var content = File.ReadAllLines(path)
                    .Skip(1)
                    .Select(x => x.Split(','))
                    .Select(x => new MyObject
                    {
                        Column1= x[0],
                        Colum2= x[1],
                        Column3 = x[2]
                    });

This worked fine for me. However, I now have a requirement to use the file upload control to select a file and then read from the file selected. I have no idea how to accomplish the same thing above. Any ideas?

6
  • this is not that difficult.. have you at least tried a google search on FileUpload Control there are tons of working examples on doing this.. also is this a WinForms || WebForms App..? FileUpload Class if you want to see how to parse a .CSV file also checkout my previous Post here Parsing CSV File Commented Dec 4, 2014 at 18:56
  • I hope you realize that once you allow the file to be uploaded, your code will have to take into account the possibility of bad data. Your code above, for example, will crash rather horribly if a line doesn't contain at least three columns, or if somebody uploads a file that isn't a CSV. Commented Dec 4, 2014 at 19:03
  • @JimMischel I do realize that thanks. I'm just in the prelimary stages of development. Once I can get the data out of the file I will worry about everything else. Commented Dec 4, 2014 at 19:17
  • @DJKRAZE I have searched google and I can't seem to find an example that reads from the selected file. I don't have the path of this file I need to bring it into memory and read the contents and then assign the contents to an object. I have yet to see an example doing this. Commented Dec 4, 2014 at 19:18
  • Looks like a duplicate of stackoverflow.com/questions/12457407/… Commented Dec 4, 2014 at 19:51

1 Answer 1

0

it is we explained in this link. You can covert it to a data table as shown in the link. Creating a DataTable from CSV File

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.