dn_learner
asked on
error when i have a different delimeter when reading a .csv file
Hi,
I have code that reads a .csv file and displays the data in a datagrid. It works fine when the delimeter is a COMMA, but gives me an error when i have a PIPE delimeter. Please help me out.
Here is the code that iam using to accomplish the task
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''Put user code to initialize the page here
Dim PathtoTextFile As String = "c:\DataFolder\"
Dim oCon As New System.Data.OleDb.OleDbCon nection("P rovider=Mi crosoft.Je t.OLEDB.4. 0;" & _
"Data Source=" & PathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES; FMT=Delimi ted""")
Dim oCmd As New System.Data.OleDb.OleDbDat aAdapter(" SELECT program, description FROM datafile.csv", oCon)
Dim dstemp As New DataSet
oCmd.Fill(dstemp)
DataGrid1.DataSource = dstemp
DataGrid1.DataBind()
End Sub
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------
Here is the error that i get:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------
Exception Details: System.Data.OleDb.OleDbExc eption: No value given for one or more required parameters.
Source Error:
Line 48:
Line 49: Dim dstemp As New DataSet
Line 50: oCmd.Fill(dstemp)
Line 51:
Line 52: DataGrid1.DataSource = dstemp
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------
I have code that reads a .csv file and displays the data in a datagrid. It works fine when the delimeter is a COMMA, but gives me an error when i have a PIPE delimeter. Please help me out.
Here is the code that iam using to accomplish the task
--------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''Put user code to initialize the page here
Dim PathtoTextFile As String = "c:\DataFolder\"
Dim oCon As New System.Data.OleDb.OleDbCon
"Data Source=" & PathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES;
Dim oCmd As New System.Data.OleDb.OleDbDat
Dim dstemp As New DataSet
oCmd.Fill(dstemp)
DataGrid1.DataSource = dstemp
DataGrid1.DataBind()
End Sub
--------------------------
Here is the error that i get:
--------------------------
Exception Details: System.Data.OleDb.OleDbExc
Source Error:
Line 48:
Line 49: Dim dstemp As New DataSet
Line 50: oCmd.Fill(dstemp)
Line 51:
Line 52: DataGrid1.DataSource = dstemp
--------------------------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There is one intresting other method also.
You can convert this csv into XML. This you can do it now with MS-Office 2003 or you need to download a utlity recently release by microsoft then can disply in datagrid.
Else you can also do it by yourself. Look into this article.
http://www.devcity.net/Articles/36/1/delimit_xml.aspx
You can convert this csv into XML. This you can do it now with MS-Office 2003 or you need to download a utlity recently release by microsoft then can disply in datagrid.
Else you can also do it by yourself. Look into this article.
http://www.devcity.net/Articles/36/1/delimit_xml.aspx
If you still not able to solve it let me know.
Sachi
Sachi
have a look at this link .. explains reading a tab delimted text file ...
Reading a Delimited File Using ASP.Net and VB.Net - Usage
http://www.dotnet247.com/247reference/a.aspx?u=http://www.devarticles.com/c/a/ASP.NET/Reading-a-Delimited-File-Using-ASP.Net-and-VB.Net/
Reading a Delimited File Using ASP.Net and VB.Net - Usage
http://www.dotnet247.com/247reference/a.aspx?u=http://www.devarticles.com/c/a/ASP.NET/Reading-a-Delimited-File-Using-ASP.Net-and-VB.Net/
ASKER
Hi guys,
i really appreicate the help you provided.
as suggested by sachiek, i read the csv file, replaced all the PIPES with a COMMA and wrote the data to a new file. And then I used that new file in my SELECT statement to read the data and displayed it into a datagrid.
I added the following code before the block of code that you see above in my original post.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------
objStreamReader = File.OpenText(MapPath("dat afile.csv" ))
strInput = objStreamReader.ReadLine()
While strInput <> Nothing
strInput = Replace(strInput, "|", ",")
objStreamWriter.Write(strI nput)
objStreamWriter.Write(objS treamWrite r.NewLine( ))
strInput = objStreamReader.ReadLine()
End While
objStreamReader.Close()
objStreamWriter.Close()
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
Please let me know if you like the code or would u have done it differently.
Thanks a lot.
i really appreicate the help you provided.
as suggested by sachiek, i read the csv file, replaced all the PIPES with a COMMA and wrote the data to a new file. And then I used that new file in my SELECT statement to read the data and displayed it into a datagrid.
I added the following code before the block of code that you see above in my original post.
--------------------------
objStreamReader = File.OpenText(MapPath("dat
strInput = objStreamReader.ReadLine()
While strInput <> Nothing
strInput = Replace(strInput, "|", ",")
objStreamWriter.Write(strI
objStreamWriter.Write(objS
strInput = objStreamReader.ReadLine()
End While
objStreamReader.Close()
objStreamWriter.Close()
--------------------------
Please let me know if you like the code or would u have done it differently.
Thanks a lot.
Try using this class.
http://www.opennetcf.org/SourceBrowse/browse.aspx?f=d:/sites/OpenNETCF/InetPub/wwwroot/Source/OpenNETCF.Data
There are two class supported for text and csv files.
Text
http://www.opennetcf.org/SourceBrowse/view.aspx?f=d:/sites/OpenNETCF/InetPub/wwwroot/Source/OpenNETCF.Data/Text/TextDataAdapter.cs
CSV
http://www.opennetcf.org/SourceBrowse/view.aspx?f=d:/sites/OpenNETCF/InetPub/wwwroot/Source/OpenNETCF.Data/CSV/CSVDataAdapter.cs
Try it out. Surely it should be working fine.
Sachi