Text to Dataset



I am trying to read in the contents of a Text file to a dataset.
Every time I run the line = "Objconn.open()" it gives me the following
exception "System.data.oledb.oledbexception: 'c:\D.txt' is not a vaild
path yada yada yada" I have changed the location of the file to many
differnt locations as well and replace the file with a different one,
No luck. I was wondering if someone out there could help. Here is my

Dim sconnectionstring As String
Dim da As OleDb.OleDbDataAdapter
sconnectionstring = "Provider=microsoft.jet.oledb.4.0;Data " &_
"source=c:\D.txt;extended properties=""Text;HDR=Yes;FMT=Delimited """
Dim objconn As New System.Data.OleDb.OleDbConnection(sconnectionstring)

Catch ex As Exception
End Try
Dim sSQL As String
sSQL = "Select * from D.txt"
da = New OleDb.OleDbDataAdapter(sSQL, objconn)
Dim ds As New DataSet("table1")
da.Fill(ds, "table1")
Dim dt As DataTable
dt = ds.Tables("table1")

Cor Ligthert

Hi John,

You can try this one (you have to set the right paths and file)

I hope it helps?


Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim file As String = "Test2.txt"
Dim path As String = "C:\Test1\"
Dim ds As New DataSet
Dim f As System.IO.File
If f.Exists(path & file) Then
Dim ConStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
path & ";Extended Properties=""Text;HDR=No;FMT=Delimited\"""
Dim conn As New OleDb.OleDbConnection(ConStr)
Dim da As New OleDb.OleDbDataAdapter("Select * from " & _
file, conn)
da.Fill(ds, "TextFile")
End If
Catch ex As Exception
End Try
DataGrid1.DataSource = ds.Tables(0)
End Sub

Val Mazur

Hi John,

Remove name of the file from the connection string. When you open
connection, then you open it against the folder, not against the file. Then
you use file name in a select statement. When you open connection, then
folder treated as a database, but files as a tables. Your connection string
should look like

sconnectionstring = "Provider=microsoft.jet.oledb.4.0;Data " &_
"source=c:\;extended properties=""Text;HDR=Yes;FMT=Delimited """


Val! I tried yours first and it worked I'm sure the others did as
well and I thank you sooo much for responding.

Thanks so much.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question