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.
Next: Code Explanation >>
More ASP.NET Articles
More By G Ajaykumar