How to import a CSV file into a Datagrid - VB.NET

G

Guest

Please help point me in the right direction to import a CSV file into a
Datagrid.

Below is the error I'm receiving, OLE csv format file, sample CSV file, and
code I've tried to no success.

I've made sure that the path is valid in windows explorer, and the NTFS
security is set to Everyone and ASP.NET full access.

Thank you in advance

ERROR:

System.Data.OleDb.OleDbException: 'C:\Inetpub\wwwroot\dev\Data\dev.csv' is
not a valid path. Make sure that the path name is spelled correctly and that
you are connected to the server on which the file resides. at
System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr) at
System.Data.OleDb.OleDbConnection.InitializeProvider() at
System.Data.OleDb.OleDbConnection.Open() at
Attachments.ImportTimco.btnReview_ServerClick(Object sender, EventArgs e) in
'C:\Inetpub\wwwroot\dev\dev.aspx.vb:line 79

CSV.TXT schema file for OleDb:

ColNameHeader=False
Format=CSVDelimited
MaxScanRows=0
CharacterSet=UNICODE
Col1=OtherID Char Width 10
Col2=ClockDate Char Width 10
Col3=Hours Char Width 5
Col4=FullName Char Width 20


CSV Data file sample:

97369,1/29/2005,8,"SAQUILON, ALFREDO"
97369,1/30/2005,8,"SAQUILON, ALFREDO"
97369,1/31/2005,7.65,"SAQUILON, ALFREDO"


CODE:

If Not File1.PostedFile Is Nothing And
File1.PostedFile.ContentLength > 0 Then
Try
Try
Dim strConnectionString As String
Dim strPathToTextFile As String
Dim tempString As String
strPathToTextFile = File1.PostedFile.FileName
strConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathToTextFile & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited"""
Dim objConnection As New
System.Data.OleDb.OleDbConnection(strConnectionString)

objConnection.Open()
Dim objDA As New OleDbDataAdapter("SELECT * FROM
CSV.txt", objConnection)

objDA.Fill(ds)

If ((ds.ToString <> "") And (ds.Tables(0).Rows.Count >
0)) Then
DataGrid1.DataSource = ds
DataGrid1.DataBind()

End If
objConnection.Close()

Catch ex As Exception
Response.Write(ex.ToString())
End Try

Catch Exc As Exception
Response.Write("Error: " & Exc.Message)
End Try
Else
Response.Write("Please select a file to upload")
End If
 
S

shriop

strPathToTextFile in your code actually includes the name of your csv
file when it shouldn't. It should only contain the path to the folder
that the csv file resides in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top