Help for a VB Newbie

Feb 1, 2012 at 10:26 PM

Hi,

After much frustration at trying to do it myself, I have loaded the KBCsv library into Visual Studio2010 and have been trying to give it a go.

From all I read, it appears to do exactly what I want!  This is my first VB project and I'm kinda learning as a go.

 

However, I'm a little stuck.  I can't see any VB code samples to see how it is used in VB.  I'm particularly keen to establish

1. how to open the file

2. how to read in the header row (because some of my 100+ CSV's dont have the correct headings)

3. how I can process each line (a little bit of logic and squirting the data off to a SQL database

 

Can anyone help?  It appears there is plenty of C examples.  I can't even find the methods.  Please dont laugh at me :)

Any help you could give would be much appreciated.

 

Thanks Houli

Coordinator
Feb 2, 2012 at 12:58 PM
Edited Feb 2, 2012 at 12:58 PM

Hi Houli,

The documentation file (CHM) available on the downloads page contains examples in both C# and VB.NET - have you checked it out? For instance, the class documentation for CsvReader includes this sample:

 

Dim reader As CsvReader = Nothing

Try
    reader = New CsvReader("C:\data.csv")
    reader.ValueSeparator = ":"c
    reader.ReadHeaderRecord()
    Dim record As DataRecord = reader.ReadDataRecord

    While (Not record Is Nothing)
        System.Console.WriteLine("{0} is {1} years old", record("Name"), record("Age"))
        record = reader.ReadDataRecord
    End While
Finally
    If (Not reader Is Nothing) Then
        reader.Close()
    End If
End Try

 

I suggest you download the CHM, have a read, and then check back again if there's something further you need help with.

Best,
Kent

Aug 12, 2014 at 5:03 PM
Kent,
I have tried the codes above in VS 2010 but I encounter a problem. Can you please help? Thanks.

Imports System.Runtime
Imports System.Data.SqlClient
Imports Kent.Boogaart.KBCsv


Public Class Form1
Private Sub ReadCSVFromFile()
    Dim reader As CsvReader = Nothing
    Try
        reader = New CsvReader("C:\Temp\test.csv")  ---> ***Error, "Overload resolution failed b/c no accessible 'New' can be called with these arguments'
        reader.ValueSeparator = ";"c
        reader.ReadHeaderRecord()
        Dim record As DataRecord = reader.ReadDataRecord

        While (Not record Is Nothing)
            System.Console.WriteLine("{0} is {1} years old", record("Name"), record("Age"))
            record = reader.ReadDataRecord
        End While
    Finally
        If (Not reader Is Nothing) Then
            reader.Close()
        End If
    End Try

End Sub
End Class
Coordinator
Aug 14, 2014 at 1:18 AM
Hi ttimkim,

That compiles fine for me. VS2010, KBCsv V2.0.2. Copied and pasted your code and built no problems.

Please confirm which version of KBCsv you are using.

Thanks,
Kent
Aug 14, 2014 at 2:11 PM
Hi Kent,
Thanks for checking the codes. I use the KBCsv-V3.0.0. Please advise. Thanks
Coordinator
Aug 15, 2014 at 3:08 PM
Hi ttimkim,

As stated on the download page for V3, it does not support older platforms, and that includes .NET 4. V3 won't have installed properly (perhaps you missed the error message when installing via NuGet?). If you need to stick with .NET 4/VS2010, you will need to use V2.0.2:
Install-Package Kent.Boogaart.KBCsv -Version 2.0.2
Best,
Kent