Solved

Reading a .CSV file with data rows having varying column counts

Posted on 2008-10-13
2
516 Views
Last Modified: 2008-10-20
I've been using this Class module for reading .csv files into my VB.Net DataTables, but I just found it will not work when the data files has rows with a varying number of columns.

The .csv file's first row has 4 columns, the second row has 5 columns, the third fow has 6 columns.

I get an error when the Sub AddRow runs since it attempts to add 5 values and first row only had 4 ??

"Cannot find column 4."

I'd like to hear your thoughts on how you would read this type of data file into a DataTable.

Thanks,
JMO9966

tblQuote_Data = fileReader.ReadFile(pathFile)

Public Function ReadFile(ByVal fileName As String) As DataTable
        ' Initialize the return values
        Dim list As New List(Of String())

        Dim table As DataTable = Nothing

        Using parser As New TextFieldParser(fileName)

            ' Setup the comma-delimited file parser.
            parser.TextFieldType = FieldType.Delimited
            parser.Delimiters = New String() {","}
            parser.HasFieldsEnclosedInQuotes = True

            While Not parser.EndOfData
                Try
                    ' Read the comma-delimited text as fields into a string array.
                    Dim input As String() = parser.ReadFields()

                    If table Is Nothing Then
                        table = Me.CreateTable(Path.GetFileName(fileName), input)
                    End If

                    If input(0).Trim <> "" Then

                        Me.AddRow(table, input)
                    Else
                        'Else send to Error table

                    End If

                Catch ex As MalformedLineException
                    ' Ignore invalid lines.
                End Try
            End While
        End Using
        Return table
    End Function

 Private Function CreateTable(ByVal name As String, ByVal input As String()) As DataTable

        Dim table As New DataTable(name)
        For index As Integer = 1 To input.Length
            table.Columns.Add("F" & index)
        Next index
        Return table
    End Function

    Private Sub AddRow(ByVal table As DataTable, ByVal input As String())

        Dim row As DataRow = table.NewRow()
        For index As Integer = 0 To table.Columns.Count
            If index < input.Length Then
                row(index) = input(index)
            End If
        Next index

        table.Rows.Add(row)
    End Sub
0
Comment
Question by:JMO9966
2 Comments
 
LVL 1

Accepted Solution

by:
Hamish_Anderson earned 500 total points
ID: 22708682
JMO, do the CSV columns have literal column names?  Ie does column 4 represent "Name" and Column 6  "Address" or similar. Or are the column names irrelevant and it is purely the order that is important?

If it is the order that is important then perhaps the structure of your table needs to be more heavily normalised.  Otherwise if there is a finite number of columns then simply create the columns, name them "col1, col2, col3" in your data table then modify the logic of your code to find the end of the csv line (this should be represented by a carriage return).
0
 

Author Comment

by:JMO9966
ID: 22762501
Thanks Harnish,

I ended up setting my For Next loop to a hard-coded number of columns since this is a static value.

The csv file does not contain a column header row.

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now