<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Techreceipe.com &#187; CSV</title>
	<atom:link href="http://www.techreceipe.com/tag/csv/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techreceipe.com</link>
	<description>Technical Tutorials,Video Tutorials on .Net and Windows</description>
	<lastBuildDate>Mon, 04 Feb 2013 13:55:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Read values from Comma-Delimited  file using VB.Net &#8211; Part 1</title>
		<link>http://www.techreceipe.com/2011/08/read-values-from-comma-delimited-file-using-vb-net-part-1/</link>
		<comments>http://www.techreceipe.com/2011/08/read-values-from-comma-delimited-file-using-vb-net-part-1/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 10:46:43 +0000</pubDate>
		<dc:creator>Techreceipe.com</dc:creator>
				<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[Read]]></category>
		<category><![CDATA[TextFieldParser]]></category>

		<guid isPermaLink="false">http://www.techreceipe.com/?p=247</guid>
		<description><![CDATA[Some application uses a comma-delimited files for storing their temporary data. Developers using Visual Basic needs to read the file by line by line and then split the value using the &#8220;,&#8221;. This was some time useful and tedious. And the .Net has the beautiful TextFieldParser Class to read the comma-delimited values using .Net. This [...]]]></description>
				<content:encoded><![CDATA[<p>Some application uses a comma-delimited files for storing their temporary data. Developers using Visual Basic needs to read the file by line by line and then split the value using the &#8220;,&#8221;. This was some time useful and tedious. And the .Net has the beautiful TextFieldParser Class to read the comma-delimited values using .Net.</p>
<p>This TextFieldParser Class is located in Microsoft.VisualBasic.IO name space of the .Net Framework.</p>
<p>The below examples read the sample CSV file using VB.Net</p>
<p>Sample File :</p>
<pre class="brush: vb; title: ; notranslate">
&quot;REVIEW_DATE&quot;,&quot;AUTHOR&quot;,&quot;ISBN&quot;,&quot;DISCOUNTED_PRICE&quot;
&quot;1985/01/21&quot;,&quot;Douglas Adams&quot;,0345391802,5.95
&quot;1990/01/12&quot;,&quot;Douglas Hofstadter&quot;,0465026567,9.95
&quot;1998/07/15&quot;,&quot;Timothy &quot;&quot;The Parser&quot;&quot; Campbell&quot;,0968411304,18.99
&quot;1999/12/03&quot;,&quot;Richard Friedman&quot;,0060630353,5.95
&quot;2001/09/19&quot;,&quot;Karen Armstrong&quot;,0345384563,9.95
&quot;2002/06/23&quot;,&quot;David Jones&quot;,0198504691,9.95
&quot;2002/06/23&quot;,&quot;Julian Jaynes&quot;,0618057072,12.50
&quot;2003/09/30&quot;,&quot;Scott Adams&quot;,0740721909,4.95
&quot;2004/10/04&quot;,&quot;Benjamin Radcliff&quot;,0804818088,4.95
&quot;2004/10/04&quot;,&quot;Randel Helms&quot;,0879755725,4.50</pre>
<p>Code:</p>
<pre class="brush: vb; title: ; notranslate">
Dim parser As New FileIO.TextFieldParser(&quot;D:\Tests.txt&quot;)
Dim rows As String()
parser.SetDelimiters(&quot;,&quot;)
parser.TextFieldType = FileIO.FieldType.Delimited
Try
'Try to read the file
'Read the Header data
rows = parser.ReadFields

'print the Header
For Each str As String In rows
Console.WriteLine(str)
Next

'Read the row data
Do While Not parser.EndOfData
rows = parser.ReadFields

'Print the Results
For Each Str As String In rows
Console.WriteLine(Str)
Next

Loop
Catch ex As FileIO.MalformedLineException
MessageBox.Show(ex.Message)

Catch ex As Exception
'Show the Error Message
MessageBox.Show(ex.Message)
End Try
</pre>
<p>This part of the code read only the header information( First line of Header file ) and</p>
<p>display them in the console window.</p>
<p>Code:</p>
<pre class="brush: vb; title: ; notranslate">
rows = parser.ReadFields
'print the Header
 For Each str As String In rows
  Console.WriteLine(str)
Next
</pre>
<p>Output:</p>
<pre class="brush: vb; title: ; notranslate">
REVIEW_DATE
AUTHOR
ISBN
DISCOUNTED_PRICE
</pre>
<p>This read the values by line by line,split them using &#8220;,&#8221; and display it in console window.</p>
<p>Code:</p>
<pre class="brush: vb; title: ; notranslate">
'Read the row data
Do While Not parser.EndOfData
rows = parser.ReadFields
'Print the Results
For Each Str As String In rows
        Console.WriteLine(Str)
      Next
Loop
</pre>
<p>Output:</p>
<pre class="brush: vb; title: ; notranslate">
1985/01/21
Douglas Adams
0345391802
5.95
1990/01/12
Douglas Hofstadter
0465026567
9.95
1998/07/15
Timothy &quot;The Parser&quot; Campbell
0968411304
18.99
1999/12/03
Richard Friedman
0060630353
5.95

</pre>
<p>Now what happens when the file is not in the proper format. This is handled by the exception module.</p>
<p>I used this text for parsing</p>
<pre class="brush: plain; title: ; notranslate">
Dim text = &quot;I, Need, Coffee, Before, I, &quot;&quot;Fall, Asleep&quot;&quot; Otherwise, &quot;&quot;I'll, Die&quot;&quot;&quot; ',
</pre>
<p>and it gave the exception as</p>
<pre class="brush: plain; title: ; notranslate">&quot;Line 1 cannot be parsed using the current Delimiters.&quot;</pre>
<p>If you still need the detailed explanation then you can use the <a title="MalFormatedException Class" href="http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.malformedlineexception.aspx" target="_blank">MalformatedLineException </a>Class</p>
<pre class="brush: plain; title: ; notranslate">

Catch ex As FileIO.MalformedLineException
MessageBox.Show(ex.Message)
</pre>
<p>&nbsp;</p>
<p>If the source file does not exist then File not found exception will be thrown , this has to be handled</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techreceipe.com/2011/08/read-values-from-comma-delimited-file-using-vb-net-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
