Solved

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

Posted on 2008-10-13
2
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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