Solved

VB.net Create a data table from a text file

Posted on 2014-11-26
3
169 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
[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
3 Comments
 
LVL 36

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 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