Link to home
Start Free TrialLog in
Avatar of chadmanvb
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("Password")
        CSVdt.Columns.Add("Description")
        Dim sr As System.IO.StreamReader
        Dim strContents() As String
        Dim strData() As String
        Try
            sr = New System.IO.StreamReader("\\scripts\windows_scripts\Easypass\passwords.csv")
            strContents = sr.ReadToEnd.Split(vbNewLine)
            sr.Close()
        Catch Ex As Exception
            Console.WriteLine(Ex.Message)
        End Try
        For Each strLine As String In strContents
            strData = strLine.Split(",")
            CSVdt.Rows.Add(strData)
        Next
    End Sub
Avatar of Hillwaaa
Hillwaaa
Flag of Australia image

Hi chadmanvb,

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
Avatar of sirbounty
If strData <> "" and strData.SubString(0,1) <> ";" Then
  strData = strLine.Split(",")
    CSVdt.Rows.Add(strData)
Avatar of chadmanvb
chadmanvb

ASKER

I am still getting blank lines when I do this and try to populate a combobox1 with this:
  Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
         Handles ComboBox1.SelectedIndexChanged
        ComboBox2.Items.Clear()
        For Each dr As DataRow In CSVdt.Select("[Branch] LIKE '*" & ComboBox1.Text & "*'")
            ComboBox2.Items.Add(dr("Description") & " " & dr("UserID"))
        Next
    End Sub
 Public Sub FillTable()
        CSVdt = New DataTable("CSV_Data")
        CSVdt.Columns.Add("Branch")
        CSVdt.Columns.Add("UserID")
        CSVdt.Columns.Add("Password")
        CSVdt.Columns.Add("Description")
        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$\cloneconfig\scripts\windows_scripts\Easypass\passwords.csv")
            strContents = sr.ReadToEnd.Split(vbNewLine)
            sr.Close()
        Catch Ex As Exception
            Console.WriteLine(Ex.Message)
        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.Trim) Then
                arr.Add(dr("Branch").ToString.Trim)
            End If
        Next
        ComboBox1.Items.AddRange(arr.ToArray)
    End Sub


using a csv file like:

;branch,userid,password,description,cics,special
UK Branch 10000,p12340,12340,FA,DUNC,Non-pilot user
UK Branch 10000,p12341,12341,OAG,NBUSKIR,Pilot User
UK Branch 10000,p12342,12342,OAG,CKETT,Non-pilot user

Canadian Branch 20000,p12343,12343,FA,DUNTX,Non-pilot user
Canadian Branch 20000,p12344,12344,OA,EAEYJ,Pilot User
Canadian Branch 20000,p12345,12345,OA,HAISMR,Pilot User

US Branch 30000,p12346,12346,FA,ELKYOK2,Pilot User
US Branch 30000,p12347,12347,OA,ELOK5,Non-pilot user
US Branch 30000,p12348,12378,OA,ELKC,Non-pilot user
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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
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
Thanks, That did it!  Chad
Not a problem, glad to help. ;=)