Text to Dataset

  • Thread starter Thread starter John
  • Start date Start date
J

John

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
Code.

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)

Try
objconn.Open()
Catch ex As Exception
MsgBox(ex.ToString)
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")
 
Hi John,

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

I hope it helps?

Cor

\\\
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
Try
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
MessageBox.Show(ex.ToString)
End Try
DataGrid1.DataSource = ds.Tables(0)
End Sub
///
 
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.
 
Back
Top