Click here to Skip to main content
14,699,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All
I am not that experienced. I am using VB Express 2008. I have a problem. I am importing a csv file in a datagrid with the following code :

Private Sub btnFindFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindFile.Click
        Dim OpenFileDialog As New OpenFileDialog
        OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        OpenFileDialog.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"
        OpenFileDialog.Title = "Select A CSV File"

        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            Dim FileName As String = IO.Path.GetFileName(OpenFileDialog.FileName)
            Dim Path As String = IO.Path.GetDirectoryName(OpenFileDialog.FileName)
            Dim connString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
                & Path & ";Extended Properties=""Text;HDR=No;FMT=Delimited"""
            Dim conn As New Odbc.OdbcConnection(connString)
            Dim da As New Odbc.OdbcDataAdapter("SELECT * FROM [" & FileName & "]", conn) 
            Dim dt As New DataTable
            da.Fill(dt)
            ItemsDataGridView.DataSource = dt
        End If
End Sub


The code is working perfectly, and display all the content of the csv file in my datagrid. My problem is the following: I cannot figure out how to save the imported info in the datagridview, to my mdb database.
I have tried the following, but with no luck :

Me.ItemsTableAdapter.Fill(Me.ItemsDataSet1.Items)
        Me.Validate()
        Me.ItemsBindingSource.EndEdit()


Any help would be appreciated.
Maybe I should just mention: I am trying to save into an existing Access mdb database, where the table has more columns than the imported csv file.
Posted

You can use File.ReadAllLines to get an array of strings representing the text in the file, then insert that into your database from there.
   
I am importing into a bound datagrid. My code is sending the info to the correct columns. The first column is my primary key. After import, the rows in the primary key column are blank. This is perfect, as when I manage to save the imported info to the existing info to the Access Database, I want the numbering of the primary key to carry on from the last row in the database.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900