Link to home
Start Free TrialLog in
Avatar of AlexPonnath
AlexPonnathFlag for United States of America

asked on

VB.Net Data Import

Hi,

i am trying to write some code which reads line by line of a text file (CSV) and trys to import them. Here is my 2 issues which i am trying to deal with.

Some fields have a ' in it like Names Walther's etc how do you gracefully handle ths ?
Also some fields have a comma in the field but then the field is enclosed by " how can i check for that ? If i just set my Spliter to , it will see the , in the field as another seperator.
Avatar of appari
appari
Flag of India image

Instead of splitting you can use ADO.Net using text driver and treat the csv/text file as a table and get the contents in to a datatable.
check this article for a sample, read the section "Using the Jet Engine"

http://www.codeproject.com/Articles/27802/Using-OleDb-to-Import-Text-Files-tab-CSV-custom
Avatar of AlexPonnath

ASKER

I dont want to use ADO.net since this function might be called for some data which will not talk to db. So thats not a solution
First of i am capable of using google myself so i have seen both of these before... i am using the streamreader already

 Dim fr As StreamReader = Nothing
 Dim FileString As String = ""
 Dim LineItemsArr() As String
 Dim FilePath As String = Trim(Me.txtSelectedFile.Text)

 fr = New System.IO.StreamReader(FilePath)
             While fr.Peek <> -1
                FileString = fr.ReadLine.Trim
                If String.IsNullOrEmpty(FileString) Then Continue While 'Empty Line              
                LineItemsArr = FileString.Split(",")
     
              End While


So what i am looking for is code  / reference to code which will handle the  folowing Line

Mc Donald's, 123, "MC Donald's, LLC"
Avatar of kaufmed
I agree: I think the TextFieldParser is a much better idea for this task. I posted a comment showing usage here:

https://www.experts-exchange.com/questions/26899233/Textfieldparser-from-CSV-file-need-to-put-into-a-datatable-or-db-and-show-user.html?anchorAnswerId=35176629#a35176629

Be sure to set the HasFieldsEnclosedInQuotes property to True!
the link i posted is not using streamreader, rather using TextFieldParser class.
read my comment again.
guys i am trying to find a solution for my problem, and the solution should not mean to get rid of streamreader. Based on the links i can not see how i can use streamreader andthe TextFieldParser class together
You said:

Some fields have a ' in it like Names Walther's etc how do you gracefully handle ths ?
Then what we have confirmed is about as graceful as you can get. Yes, you can achieve your goal by continuing on with the StreamReader and writing some logic to account for the commas and quotations, but what you are effectively doing is reinventing a parser. Why do that? From the code you posted, I cannot see what benefit you hope to gain by sticking with a StreamReader. If there is some specific reason why you need a StreamReader, then perhaps you would share that reason with us.

Based on the links i can not see how i can use streamreader andthe TextFieldParser class together
Well, there is an overload for the constructor which takes in a TextReader object (from which StreamReader derives).

e.g

Dim tfp As New TextFieldParser(fr)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial