Solved

VB.net Create a data table from a text file

Posted on 2014-11-26
3
168 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
ID: 40470096
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
ID: 40470265
Thanks very much
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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