Home arrow ASP.NET arrow Page 4 - Reading a Delimited File Using ASP.Net and VB.Net
ASP.NET

Reading a Delimited File Using ASP.Net and VB.Net


New to .NET? Wondering how to split contents using a delimiter? No problem, this latest article from Ajaykumar shows you just how easy reading the contents of a delimited file can be.

Author Info:
By: G Ajaykumar
Rating: 4 stars4 stars4 stars4 stars4 stars / 223
October 15, 2003
TABLE OF CONTENTS:
  1. · Reading a Delimited File Using ASP.Net and VB.Net
  2. · Usage
  3. · Example Code
  4. · Example Code, Cont'd
  5. · Code Explanation
  6. · Output

print this article
SEARCH DEVARTICLES

Reading a Delimited File Using ASP.Net and VB.Net - Example Code, Cont'd
(Page 4 of 6 )

After we’ve specified the delimiter, we now split the contents of the files using the specified delimiter.  In order to do that we’ll declare a variable named splitout.  We’ll then use the 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.

The Split function contains two parameters: the first is the content, the second is the delimiter.  The contents of the file are split according to the delimiter passed.  The split contents are then stored in the form of arrays.  To exhibit the output we will use two asp: labels, whose text will be assigned dynamically.  Let us place a label out of the </script> tag

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

The above tag should be placed outside of the </script> tag.

Now we declare a variable i with data-type 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 may be wondering how splitout has become an array.  Whenever you use the split function, the variable that is used for splitting is automatically converted into 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, using lblsplittext.text we dynamically assign the value to the asp:label named lblsplittext.  We’ll take a look at the next section of that code.

Here we use the <b> (bold) tag, which is a normal HTML tag.  Here, it’s simply used to bold the text.  We then concatenate the variable i which is in the loop (i displays a serial number)

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

In the next part

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

We are concatenating the variable splitout (i), which 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 an array starts 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 this:

splitout(0), then splitout(1), then splitout(2) until the upper limit.

To navigate to the next array element using the for loop then we use the Next key word.  In order to differentiate the file contents and the read delimited contents, we shall display the files contents to an 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 label’s text with the contents read from the file.

lblplaintext.text = readcontents & "<br>"

In the code above, we concatenate a “<br>” just to break the line (for readability purposes).  Now we close the stream class objecft in order to release the resources:

filestream.Close()  

After we close the stream object, we close the subroutine, and close the script tag:

End Sub
</script>

After closing the script tag we will be placing the asp: label (as previously explained) 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 create two asp: labels, whose text are assigned dynamically, which are merely used for display purposes.

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


blog comments powered by Disqus
ASP.NET ARTICLES

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials