Solved

VB.net Create a data table from a text file

Posted on 2014-11-26
3
167 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Programmatically signing Word macros 4 72
Populating an array of locale_time_info 8 46
Regex validation 2 24
Datagridview column when exported to Excel changes vb.net 1 13
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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