Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I have some code that reads csv files and then copies the contents to a DataTable. I have created a file dialog so I can select the spreadsheet that I wish to import, however am having trouble figuring out how to do this. The file location and name are stored in a read only text box.

I have tried changing the [Apr2013.csv] to ["& txtImportSpreadsheet.text &"] ect and have also tried the same with the variable folder.

VB
Dim folder = "N:\access\Telephones\"
Dim con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
Dim dt As New DataTable

Using Adp As New OleDbDataAdapter("Select * From [Apr2013.csv]", con)
Adp.Fill(dt)
End Using


If anyone could help me out I would really be grateful.
Posted

Also try String.Format("This text to add a file named: [{0}]", filename)

where filename is a string variable, to added into the main string. Yay

LG out
 
Share this answer
 
Solution 1 is faulty. The loop logic doesn't work because on the last valid line, the string isn't empty so it loops again but then immediately empties the string making it fail at the latter split stage.

Also, he dims "i As Long" but doesn't use it.

Try this

Dim SR As StreamReader = New StreamReader("your csv file path")
        Dim line As String = SR.ReadLine()
        Dim strArray As String() = line.Split(","c)
        Dim dt As DataTable = New DataTable()
        Dim row As DataRow

        For Each s As String In strArray
            dt.Columns.Add(New DataColumn())
        Next

        Do
            line = SR.ReadLine
            If Not line = String.Empty Then
                row = dt.NewRow()
                row.ItemArray = line.Split(","c)
                dt.Rows.Add(row)
            Else
                Exit Do
            End If
        Loop
 
Share this answer
 
Comments
Tomas Takac 22-Feb-15 12:21pm    
Please note that the question is from May 2013!
VB
Dim SR As StreamReader = New StreamReader("1.txt")
               Dim i As Long = 0
               Dim line As String = SR.ReadLine()
               Dim strArray As String() = line.Split(",")
               Dim dt As DataTable = New DataTable()
               Dim row As DataRow

               For Each s As String In strArray
                   dt.Columns.Add(New DataColumn())
               Next
               Do
                   line = SR.ReadLine
                   row = dt.NewRow()
                   row.ItemArray = line.Split(",")
                   dt.Rows.Add(row)


               Loop While Not line = String.Empty



In datatable dt you will have all values....!
 
Share this answer
 
v2
Comments
IanMac91 31-May-13 7:39am    
Hi
Thanks for your help!

I keep getting an error now "Object reference not set to an instance of an object." on "row.ItemArray = line.Split(",")". This is how I probably would have written it as well so can't think of how to correct it.
Member 10230728 25-Aug-13 8:14am    
try row.itemArray = line.split(","c)

Add the little c to converts it to a char, and it should work.
insted of
VB
Dim row As DataRow

Write
VB
Dim row As New DataRow
 
Share this answer
 
Your code looks like it should work fine.
Put a try catch loop around the Using Adp -- End Using
Its likely not working its because your having an uncaptured error!
 
Share this answer
 

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