Solved

VB.net Create a data table from a text file

Posted on 2014-11-26
3
164 Views
Last Modified: 2014-11-28
Hi
I am trying to create a DataTable from a text file such as the one attached .
The code seems to run fine but the data table seems to be blank

Sub Create_DataTable

            Dim dt As DataTable = GetDataTableFromCsv(sFileAndPath, False)

    End Sub


    Private Shared Function GetDataTableFromCsv(path__1 As String, isFirstRowHeader As Boolean) As DataTable
        Dim header As String = If(isFirstRowHeader, "Yes", "No")

        Dim pathOnly As String = Path.GetDirectoryName(path__1)
        Dim fileName As String = Path.GetFileName(path__1)

        Dim sql As String = "SELECT * FROM [" & fileName & "]"

        Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & pathOnly & ";Extended Properties=""Text;HDR=" & header & """")
            Using command As New OleDbCommand(sql, connection)
                Using adapter As New OleDbDataAdapter(command)
                    Dim dataTable As New DataTable()
                    dataTable.Locale = CultureInfo.CurrentCulture
                    adapter.Fill(dataTable)
                    Return dataTable
                End Using
            End Using
        End Using
    End Function

Open in new window

0
Comment
Question by:murbro
3 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
Comment Utility
You are missing  the "FMT=Delimited" property in the provider string:
Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & pathOnly & ";Extended Properties=""text;HDR=" & header &";FMT=Delimited"";")

Open in new window

Also check that your file path is correct (e.g. File. Exists method), you may be surprised how many times you may have a typo.
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks very much
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

19 Experts available now in Live!

Get 1:1 Help Now