Click here to Skip to main content
16,012,611 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Validate a CSV file in VB.NET

Rate me:
Please Sign up or sign in to vote.
3.38/5 (5 votes)
23 Oct 2011CPOL 31.9K   5   2
Code to validate a CSV file in VB.NET.

Here is how to validate a CSV file:


VB
Dim reader As StreamReader = New StreamReader(strFilePath)
Try
    Dim j As Integer = 0
    Dim i As Integer

    If reader.EndOfStream Then
        MessageBox.Show("The CSV file is empty, please update", _
                        "Error", MessageBoxButtons.OK)
        Application.Exit()
    End If

    While Not reader.EndOfStream
        'Dim s As String = reader.ReadLine()
        Dim lineContents() As String = Split(reader.ReadLine(), ",")
        Dim NoOfCol As Integer

        j = j + 1
        If j = 1 Then
            NoOfCol = lineContents.Length
        End If
        If NoOfCol = lineContents.Length Then
            For i = 0 To lineContents.Length - 1
                'Dim str As String = lineContents(i)
                If Trim(lineContents(i)).Length = 0 Then
                    'alert user here.... and exit???
                    Dim Response As DialogResult = _
                       MessageBox.Show("Null value found at row " & j & _
                        ", column " & (i + 1) & vbCrLf & _
                        "Do you want to Continue?", _
                        "CSV File Confirmation", MessageBoxButtons.YesNo)
                    If Response = DialogResult.No Then
                        Application.Exit()
                    End If
                End If
            Next
        Else
            MessageBox.Show("Error: Row " & j & _
              " has invalid number of columns", _
              "CSV File Error", MessageBoxButtons.OK)
            Application.Exit()
        End If
    End While

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Oman Oman
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 as per the (only so far) comment bel... Pin
Member 810780625-Oct-11 0:12
Member 810780625-Oct-11 0:12 
GeneralThis is very limited to a specific flavor of CSV (ex. ones t... Pin
Andrew Rissing24-Oct-11 17:41
Andrew Rissing24-Oct-11 17:41 
This is very limited to a specific flavor of CSV (ex. ones that don't include, comment rows, quoted columns, etc.) and relies on MessageBoxes and Application.Exit to control flow.

If you're going to go through the hassle of parsing it to validate it, why not get the parsed values as well using one of the many parsers available from Codeproject or the internet?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.