Problem accessing text file through OleDb

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

I cannot get the followin to code to work:

Const str_datafile As String = "F:\T"

Private connKunde1 As OleDb.OleDbConnection
Private dadapKunde1 As OleDb.OleDbDataAdapter
Private dacmdKunde1 As OleDb.OleDbCommand
Private datsetKunde1 As DataSet

Private Sub form_datatest_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load

Dim str_sql As String = "SELECT * FROM
KunddatTest.txt"

connKunde1 = New OleDb.OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=" & str_datafile & ";" _
& "Extended
Properties=""Text;HDR=YES;FMT=Delimited""")
connKunde1.Open()

dadapKunde1 = New OleDb.OleDbDataAdapter(str_sql,
connKunde1)
dadapKunde1.TableMappings.Add
("Table", "KunddatTest.txt")

datsetKunde1 = New DataSet

' line with error
dadapKunde1.Fill(datsetKunde1, "KunddatTest.txt")

Me.datagrid_kunde_1.DataSource = datsetKunde1.Tables
("KunddatTest.txt")

End Sub

The error is: "An unhandled exception of
type 'System.Data.OleDb.OleDbException' occurred in
system.data.dll"

In the directory F:\T I have a Schema.ini file (don't
know if it is even needed):
[KunddatTest.txt]
ColNameHeader=True
Format=CSVDelimited
MaxScanRows=100
CharacterSet=ANSI


Any suggestions as to what the problem might be, will
much appreciated.



With kind regards,

Frank
 
Frank,

Your code looks good. The error you are getting is a generic error because you are not catching the real error. To do that modify your code as follows:

Change:
dadapKunde1.Fill(datsetKunde1, "KunddatTest.txt")

to

Try
dadapKunde1.Fill(datsetKunde1, "KunddatTest.txt")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Now you should see more descriptive error and most likely you would be able to fix that.

As far as the Schema.ini, it is not needed but if you have it, it will be better so that you define data types for all your columns.

If you still have problems, let me know.


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 
Thanks Hussein, that solved it.

Regards,

Frank
-----Original Message-----
Frank,

Your code looks good. The error you are getting is a
generic error because you are not catching the real
error. To do that modify your code as follows:
Change:
dadapKunde1.Fill(datsetKunde1, "KunddatTest.txt")

to

Try
dadapKunde1.Fill
(datsetKunde1, "KunddatTest.txt")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Now you should see more descriptive error and most
likely you would be able to fix that.
As far as the Schema.ini, it is not needed but if you
have it, it will be better so that you define data types
for all your columns.
If you still have problems, let me know.


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft
Strategic Technology Protection Program and to order your
FREE Security Tool Kit, please visit
 
Back
Top