|
-
Oct 9th, 2012, 11:26 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] TextFieldParser ReadFields call by name instead of position
Is it possible to use TextFieldParser and call .ReadFields and then use field name instead of field position?
For example, I have 40 columns in my .csv file.
field 0 = ID
field 1 = brand
field 2 = model
field 3 = price
field 4 = color
field 5 = something else.....
How the data would appear in the file:
ID,brand,model,price,color,somethingelse
1,Samsung,Galaxy S,200,black,xx
2,Samsung,Galaxy S II,300,black,xy
3,Samsung,Galaxy S III,500,black,xy
4,Samsung,Galaxy S III,500,white,xy
Right now I am calling the array like so CurrentRecord(4) which returns the color field value.
Can I call it like so, CurrentRecord("color") which would return the same value?
Code:
Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(sFullPath)
Dim CurrentRecord As String() ' this array will hold each line of data
afile.TextFieldType = FileIO.FieldType.Delimited
afile.Delimiters = New String() {","}
afile.HasFieldsEnclosedInQuotes = True
' parse the actual file
Dim iRow As Integer = 1
Do While Not afile.EndOfData
Try
CurrentRecord = afile.ReadFields
If iRow = 1 Then
'headers
Else
If CurrentRecord(39).Trim.Length > 0 Then
Dim sRetVal As String = InsertUpdateDB(CurrentRecord(0), CurrentRecord(19), CurrentRecord(20), _
CurrentRecord(21), CurrentRecord(21), CurrentRecord(22), _
CurrentRecord(23), CurrentRecord(24), CurrentRecord(25), _
CurrentRecord(26), CurrentRecord(27), CurrentRecord(28), _
CurrentRecord(29), CurrentRecord(30), CurrentRecord(31), _
CurrentRecord(32), CurrentRecord(33), CurrentRecord(34), _
CurrentRecord(35), CurrentRecord(36), CurrentRecord(37), _
CurrentRecord(38), CurrentRecord(39))
Else
Dim sRetVal As String = InsertUpdateDB(CurrentRecord(0), CurrentRecord(19), CurrentRecord(20), _
CurrentRecord(21), CurrentRecord(21), CurrentRecord(22), _
CurrentRecord(23), CurrentRecord(24), CurrentRecord(25), _
CurrentRecord(26), CurrentRecord(27), CurrentRecord(28), _
CurrentRecord(29), CurrentRecord(30), CurrentRecord(31), _
CurrentRecord(32), CurrentRecord(33), CurrentRecord(34), _
CurrentRecord(35), CurrentRecord(36), CurrentRecord(37), _
CurrentRecord(38), 0)
End If
End If
Catch ex As FileIO.MalformedLineException
ltlOutput.Text &= "<br />" & ex.ToString
Stop
End Try
iRow += 1
Loop
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|