Hi
Being new to
VB.Net I am struggling with the following.
I want to load the contents of a text file into a datagrid (by browsing to the text file at runtime using a file dialog box).
I have been trying with System.Data.OleDB, though have read that using System.IO namespace is also possible. Any help or guidance appreciated.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New OleDb.OleDbConnection
Dim strFileName As String
With fileDialog
.Title = "Import CSV file"
.InitialDirectory = "C:\"
.Filter = "Sage (*.xls;*.csv;*.txt)|*.xls;*.csv;*.txt|All files (*.*)|*.*"
.ShowDialog()
strFileName = .FileName
End With
Try
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\;" _
& "Extended Properties=""text;" _
& "HDR=No;" _
& "FMT=Delimited"""
conn.Open()
'Will pass the filename here when I get it working
Dim adap As New OleDb.OleDbDataAdapter("select * from txtFile.txt", conn)
Dim dstSage As New DataSet
'adap.Fill(dstSage, "batch")
dg.DataSource = adap.Fill(dstSage, "batch")
dg.Refresh()
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
End Sub