skhurams
skhurams

Reputation: 2145

Importing csv values having multiple comma in one column in Asp.net

I am having an issue with importing a CSV file. The problem arises when an address field has multiple comma seperated values e.g. home no, street no, town etc.

I tried to use http://forums.asp.net/t/1705264.aspx/1 this article but, the problem did not solved because of a single field containing multiple comma separated values.

Any idea or solution? because I didnt found any help

Thanks

Upvotes: 1

Views: 2723

Answers (3)

Jeff Maner
Jeff Maner

Reputation: 1179

Are the fields surrounded by quotation marks? If so, split on "," rather than just ,.

Is the address field at the beginning or end of a record? If so, you can ignore the first x commas (if at the beginning) or split only the correct number of fields (if at the end).

Ideally, if you have control of the source file's creation, you would change the delimiter of either the address sub-fields, or the record fields.

Upvotes: 0

joshgo
joshgo

Reputation: 1183

Don't split the string yourself. Parsing CSV files is not trivial, and using str.Split(',') will give you a lot of headaches. Try using a more robust library like CsvHelper - https://github.com/JoshClose/CsvHelper

If that doesn't work then the format of the file is probably incorrect. Check to make sure the text fields are properly quoted.

Upvotes: 2

Devin Goble
Devin Goble

Reputation: 2867

Do you control the format of the CSV file? You could see about changing it to qualify the values by surrounding them with double quotes (""). Another option is to switch to a different delimiter like tabs.

Barring that, if the address is always in the same format, you could read in the file, and then inspect each record and manually concatenate columns 2, 3, and 4.

Upvotes: 0

Related Questions