.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

I Tried to Read a CSV in a DataTable, but i've got error

Posted By: jean robert     Posted Date: August 19, 2011    Points:5   Category :VB.Net
hello, i'm new in vb.net, i'm trying to do this like 2 days ago:
the user select a file CSV to import from his computer,

i've done it, the file is saved in tmp folder with a new name.

now i need to read this file to verify if the data is correct.
i looked in some other foruns, and then i got this code:

If System.IO.File.Exists(strFileNamePath) Then
End If

dtbArquivo.Columns.Add("matricula_sistema", Type.GetType("System.String"))
dtbArquivo.Columns.Add("codigo_modulo", Type.GetType("System.Int32"))
dtbArquivo.Columns.Add("nome_publico", Type.GetType("System.String"))
dtbArquivo.Columns.Add("nome_privado", Type.GetType("System.String"))
dtbArquivo.Columns.Add("comentarios", Type.GetType("System.String"))
dtbArquivo.Columns.Add("gravar_log", Type.GetType("System.String"))

'cria um novo objeto StreamReader

Dim objReader As New StreamReader(strFileNamePath)

'percorre o arquivo

Do While objReader.Peek() <> -1

strLinhaTexto = objReader.ReadLine()

arrLinhas = Split(strLinhaTexto, ";")

dtrRow = dtbArquivo.NewRow

dtrRow.ItemArray = arrLinhas



ok... but my arrLinhas returns more columns than the datatable contains, and filled with codes like " PK   ! q9+p ?  ?[Content_Types].xml ??(? "

i really dont know why, anyone can help me with this?

sorry for my bad english, i just know a few words, i'm brazilian.

Obs.: my mate said maybe i need to use some 'encoding' property, but i don't know what is this/how to do.

i'll be online in msn messenger: jeanralves@hotmail.com
and checking this forum

thanks a lot.

Author: Gowthammanju             
Posted Date: August 20, 2011     Points: 20   

try these coding as

Public Function GetCsvData(ByVal strFolderPath As String, ByVal strFileName As String) As DataTable

Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFolderPath & ";Extended Properties=Text;"
Dim conn As New OleDbConnection(strConnString)

Dim cmd As New OleDbCommand("SELECT * FROM [" & strFileName & "]", conn)
Dim da As New OleDbDataAdapter()

da.SelectCommand = cmd

Dim ds As New DataSet()


Return ds.Tables(0)
Return Nothing
End Try

End Function

Thanks & Regards,

Mark if the answer is true

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend