chadmanvb
asked on
skip blank lines or lines that start with a ; when reading csv file
I am reading a csv file to setup a table. I want it to ignore any line that is blank or starts with a ;
below is my current code:
Public Sub FillTable()
CSVdt = New DataTable("CSV_Data")
CSVdt.Columns.Add("Branch" )
CSVdt.Columns.Add("UserID" )
CSVdt.Columns.Add("Passwor d")
CSVdt.Columns.Add("Descrip tion")
Dim sr As System.IO.StreamReader
Dim strContents() As String
Dim strData() As String
Try
sr = New System.IO.StreamReader("\\ scripts\wi ndows_scri pts\Easypa ss\passwor ds.csv")
strContents = sr.ReadToEnd.Split(vbNewLi ne)
sr.Close()
Catch Ex As Exception
Console.WriteLine(Ex.Messa ge)
End Try
For Each strLine As String In strContents
strData = strLine.Split(",")
CSVdt.Rows.Add(strData)
Next
End Sub
below is my current code:
Public Sub FillTable()
CSVdt = New DataTable("CSV_Data")
CSVdt.Columns.Add("Branch"
CSVdt.Columns.Add("UserID"
CSVdt.Columns.Add("Passwor
CSVdt.Columns.Add("Descrip
Dim sr As System.IO.StreamReader
Dim strContents() As String
Dim strData() As String
Try
sr = New System.IO.StreamReader("\\
strContents = sr.ReadToEnd.Split(vbNewLi
sr.Close()
Catch Ex As Exception
Console.WriteLine(Ex.Messa
End Try
For Each strLine As String In strContents
strData = strLine.Split(",")
CSVdt.Rows.Add(strData)
Next
End Sub
If strData <> "" and strData.SubString(0,1) <> ";" Then
strData = strLine.Split(",")
CSVdt.Rows.Add(strData)
strData = strLine.Split(",")
CSVdt.Rows.Add(strData)
ASKER
I am still getting blank lines when I do this and try to populate a combobox1 with this:
Private Sub ComboBox1_SelectedIndexCha nged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles ComboBox1.SelectedIndexCha nged
ComboBox2.Items.Clear()
For Each dr As DataRow In CSVdt.Select("[Branch] LIKE '*" & ComboBox1.Text & "*'")
ComboBox2.Items.Add(dr("De scription" ) & " " & dr("UserID"))
Next
End Sub
Public Sub FillTable()
CSVdt = New DataTable("CSV_Data")
CSVdt.Columns.Add("Branch" )
CSVdt.Columns.Add("UserID" )
CSVdt.Columns.Add("Passwor d")
CSVdt.Columns.Add("Descrip tion")
CSVdt.Columns.Add("CICS")
CSVdt.Columns.Add("Special ")
Dim sr As System.IO.StreamReader
Dim strContents() As String
Dim strData() As String
Try
sr = New System.IO.StreamReader("\\ nfdgnode-1 \cloneout$ \cloneconf ig\scripts \windows_s cripts\Eas ypass\pass words.csv" )
strContents = sr.ReadToEnd.Split(vbNewLi ne)
sr.Close()
Catch Ex As Exception
Console.WriteLine(Ex.Messa ge)
End Try
For Each strLine As String In strContents
If strLine.StartsWith(";") Or strLine.Length = 0 Then
'skipping(line)
Else
strData = strLine.Split(",")
CSVdt.Rows.Add(strData)
End If
Next
End Sub
Private Sub GetUniqueBranches()
Dim arr As New ArrayList
For Each dr As DataRow In CSVdt.Rows
If Not arr.Contains(dr("Branch"). ToString.T rim) Then
arr.Add(dr("Branch").ToStr ing.Trim)
End If
Next
ComboBox1.Items.AddRange(a rr.ToArray )
End Sub
using a csv file like:
;branch,userid,password,de scription, cics,speci al
UK Branch 10000,p12340,12340,FA,DUNC ,Non-pilot user
UK Branch 10000,p12341,12341,OAG,NBU SKIR,Pilot User
UK Branch 10000,p12342,12342,OAG,CKE TT,Non-pil ot user
Canadian Branch 20000,p12343,12343,FA,DUNT X,Non-pilo t user
Canadian Branch 20000,p12344,12344,OA,EAEY J,Pilot User
Canadian Branch 20000,p12345,12345,OA,HAIS MR,Pilot User
US Branch 30000,p12346,12346,FA,ELKY OK2,Pilot User
US Branch 30000,p12347,12347,OA,ELOK 5,Non-pilo t user
US Branch 30000,p12348,12378,OA,ELKC ,Non-pilot user
Private Sub ComboBox1_SelectedIndexCha
Handles ComboBox1.SelectedIndexCha
ComboBox2.Items.Clear()
For Each dr As DataRow In CSVdt.Select("[Branch] LIKE '*" & ComboBox1.Text & "*'")
ComboBox2.Items.Add(dr("De
Next
End Sub
Public Sub FillTable()
CSVdt = New DataTable("CSV_Data")
CSVdt.Columns.Add("Branch"
CSVdt.Columns.Add("UserID"
CSVdt.Columns.Add("Passwor
CSVdt.Columns.Add("Descrip
CSVdt.Columns.Add("CICS")
CSVdt.Columns.Add("Special
Dim sr As System.IO.StreamReader
Dim strContents() As String
Dim strData() As String
Try
sr = New System.IO.StreamReader("\\
strContents = sr.ReadToEnd.Split(vbNewLi
sr.Close()
Catch Ex As Exception
Console.WriteLine(Ex.Messa
End Try
For Each strLine As String In strContents
If strLine.StartsWith(";") Or strLine.Length = 0 Then
'skipping(line)
Else
strData = strLine.Split(",")
CSVdt.Rows.Add(strData)
End If
Next
End Sub
Private Sub GetUniqueBranches()
Dim arr As New ArrayList
For Each dr As DataRow In CSVdt.Rows
If Not arr.Contains(dr("Branch").
arr.Add(dr("Branch").ToStr
End If
Next
ComboBox1.Items.AddRange(a
End Sub
using a csv file like:
;branch,userid,password,de
UK Branch 10000,p12340,12340,FA,DUNC
UK Branch 10000,p12341,12341,OAG,NBU
UK Branch 10000,p12342,12342,OAG,CKE
Canadian Branch 20000,p12343,12343,FA,DUNT
Canadian Branch 20000,p12344,12344,OA,EAEY
Canadian Branch 20000,p12345,12345,OA,HAIS
US Branch 30000,p12346,12346,FA,ELKY
US Branch 30000,p12347,12347,OA,ELOK
US Branch 30000,p12348,12378,OA,ELKC
Sorry - the strLine length check needs to first call Trim to remove the end of line characters:
For Each strLine As String In strContents
If strLine.StartsWith(";") Or strLine.Trim.Length = 0 Then
'skipping(line)
Else
strData = strLine.Split(",")
CSVdt.Rows.Add(strData)
End If
Next
For Each strLine As String In strContents
If strLine.StartsWith(";") Or strLine.Trim.Length = 0 Then
'skipping(line)
Else
strData = strLine.Split(",")
CSVdt.Rows.Add(strData)
End If
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, That did it! Chad
Not a problem, glad to help. ;=)
try this as your For Each loop:
For Each strLine As String In strContents
If strLine.StartsWith(";") Or strLine.Length = 0 Then
' skipping line
Else
strData = strLine.Split(",")
CSVdt.Rows.Add(strData)
End If
Next
Cheers,
Hillwaaa