Reading a Delimited File Using ASP.NET and VB.NET
page 3 of 4
by G Ajaykumar
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 41493/ 31

Reading a Delimited Text File

We have seen what a delimited text file is, what is the important usage and applications of these delimited text files. Now we shall go on and see how to read these delimited text files using ASP.NET and VB.NET

Note : delimiter’s sometimes are called a splitter

To read files using .NET we need to declare a namespace called SYSTEM.IO, which contains a set of classes, and methods, which is used for reading as well as writing files.

Let us now declare a name space, which will have System.IO

 

<%@Import Namespace="System.IO"%>

 

            Next we open the script tag, which will run at server, let us now open

the script tag ,we will be specifying our scripting language as vb in the tag

 

<Script language="vb" runat="server">

 

            Next we shall think about where to write the function, we shall write coding when the page loads, we shall declare the Subroutine, which fires when the page loads. Let us now declare the subroutine

 

            Sub page_load (Sender As Object,e As EventArgs)

 

We need to specify a file name which is to be read, whether it is a text file or a

. csv file, file name has to be assigned to a variable, let us now declare a

Variable named filetoread, which will be used to store the filename.

            Dim filetoread as string

Before we assign the file name to the variable, we will be using a server

Variable, which is used to trace the path of the specified file. Using

Server.mappath we can trace the absolute path of the file, which is specified.

            filetoread=server.mappath("readtest.txt")

In the above code we map the path of the text file readtest.txt where the path is

Stored into the variable filetoread.

Next we need to initiate the Stream Reader class, where we declare

filestream as a stream reader.

            Dim filestream as StreamReader

We need to assign a file to the streamreader to open the file,

            filestream = File.Opentext(filetoread)

In the above code we use Opentext method of the streamer and pass the variable filetoread, the variable filetoread contains the filepath.

Streamer reads the content, now we need a place to store the

Contents for the file, we will have to declare a variable named readcontents with Data type as string, which will store the contents of the file

            Dim readcontents as String

Using the ReadToEnd method of the streamer we read the entire contents of to the variable readcontents

 

readcontents = fileStream.ReadToEnd()

 

Now we will have to declare a variable, which will store the delimiter, that is the separator text, let us declare the variable named as textdelimiter with the data type as string.

 

Dim textdelimiter as String

 

Now the time has come for us to assign the delimiter, which might be a letter, word, number etc. Now let us assign the delimiter to be “geeyes”

 

Textdelimiter = "geeyes"

 

in the above code you can change the value of the variable textdelimiter as you wish, for example if you wish to change the delimiter to “#” your variable will look like this

 

Textdelimiter = “#” .

 

If you want to read a space delimited text file then you will need to provide a space between the double quotes of the variable as shown

 

Textdelimiter = “ “

 

Note: Value of variable is mandatory and is very important, as the reading of delimited file depends on value that is stored in the variable, which is the delimiter.

 

After we have specified the delimiter, now the time has come to split the contents of the files according to the delimiter, which has been specified. to do that first we shall declare a variable named splitout , then we will be using split function to split the text .

                                   

Dim splitout = Split(readcontents,textdelimiter)

   

In the above code we have declared a variable named splitout, to that variable we are assigning split functionality

Split (readcontents, textdelimiter)

 

Split function contains two parameters one is the content and the split text (delimiter).

In the above script we have readcontents variable, which contains the contents, then we pass the variable textdelimiter, which contains the delimiter text.

Passing two variables the contents are split according to the delimiter passed.

The split contents are stored in the form of arrays.

To exhibit the output we will be two asp: label whose text will be assigned dynamically

Let us place a label out of the </script> tag

 

<asp:label runat="server" id="lblsplittext">

 

Note: the above tag should be placed out of the </script> tag.

 

Now we declare a variable i with datatype as integer which is used for looping.

    dim i as integer

 

Now we will be using a for loop to read the contents which are stored in the arrays .

 

    for i=0 to Ubound(splitout)

 

in the above code Ubound(splitout) specifies the upper limit of the array splitout, splitout is the variable which contains the split text. Here you might wonder how splitout has become an array, when ever you use a split function the variable that is used for splitting is automatically converted into array. Here in our case splitout is automatically converted as an array.

 

Starting from zero which is our lower limit until the upper limit we write the text to the asp:label .

 

lblsplittext.Text &= "<b>Split </b>" & i+1 & ")   " & splitout(i)& "<br>"

 

in the above code lblsplittext.text , we dynamically assign the value to the asp:label named lblsplittext  let us have a look of the next part.

 

Here we use <b> bold tag which is a normal HTML tag, which is just used to brighten the test then we concatenate the variable i which is in loop , which displays serial number.

 

"<b>Split </b>" & i+1 & ")  

 

in the next part

 

" & splitout(i)& "<br>"

 

We are concatenating the variable splitout (i), we would see that variable i is passed to the splitout variable, here the concept of passing i is as follows,

we know that splitout is an array, the lower limit of and array start from zero, since this splitout(i) is within the loop, the variable passes from the lower limit to the upper limit, when it goes through loop it looks like, splitout(0), then splitout(1), then splitout(2) until the upper limit.

To navigate to the next array using the for loop then we use the key word

Next

 

In order to differentiate the file contents and the read delimited contents, we shall display the files contents to a asp: label directly which is raw without any split.

Now let us place the tag after the </script> tag

<asp:label runat="server" id="lblplaintext">

 

we dynamically assign the labels text with the contents read from the file.

 

lblplaintext.text = readcontents & "<br>"

  in the above code we concatenate a “<br>” just to break the line.

Now we close the stream class , to release the resources

filestream.Close()              

after we close the stream class we close the subroutine

End Sub

Next we close the script tag

</script>

After closing the script tag we will be placing the asp: label  as explained previously and some text.

<b>Plain Output</b><br />

<Asp: label runat="server" id="lblplaintext" Font-Name="Verdana" />

 

<b>Split Output</b><br />

<Asp: label runat="server" id="lblsplittext" Font-Name="Verdana" />

 

the above tags creates two asp: labels which is merely used for display purpose whose text are assigned dynamically.

 

That’s it…. We have gone through the complete procedure for reading a delimited text file.


View Entire Article

User Comments

Title: Life Saver and excellent article   
Name: Muzaffar Sadiq
Date: 2007-12-14 7:34:01 AM
Comment:
I really appreciate if you could tell me that after reading data from a delimited file, how I can put data (value) in SharePoint list?

Thank you
Title: Excellent article!!   
Name: Cilina
Date: 2007-12-10 8:52:57 PM
Comment:
This is an excellent article. It works out so pretty :))))))))
Title: Localisation Manager   
Name: JaimeHy
Date: 2007-07-20 3:48:36 PM
Comment:
Nice article. Detailed enough explanation.

Very complete.

Thanks!
Title: Check out this library   
Name: Netra
Date: 2007-02-25 3:30:50 PM
Comment:
forgot to post URL!

http://vishalseth.com/blog/vishaltextfileutils/

there it is.

this library does everything with delimited files. it can convert to and from any format, open, write, read, reorder columns. Very usefull.
Title: Check out this library   
Name: Netra
Date: 2007-02-25 3:29:37 PM
Comment:
this library does everything with delimited files. it can convert to and from any format, open, write, read, reorder columns. Very usefull.
Title: very good   
Name: a visitor
Date: 2006-12-20 10:08:11 AM
Comment:
a same copy can be found out also at the link below ...
http://www.devarticles.com/c/a/ASP.NET/Reading-a-Delimited-File-Using-ASP.Net-and-VB.Net/
Title: how to read a delimited text file using asp.net   
Name: Dung vu
Date: 2006-11-28 12:09:38 PM
Comment:
It is very interesting article. How can I insert this array into SQL database? Please Help. Thank you.

Dung Vu
vincenthuyvu@sbcglobal.net

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 8:59:03 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search