Home arrow ASP.NET arrow Page 3 - 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
(Page 3 of 6 )

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

Let us now declare a namespace with the name of System.IO:

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

Next, we open the script tag, which will run on the server.  We also specify 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, and 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 filename which is to be read – whether it is a text file or a . csv file, the filename 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 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 in order to open that file:  

filestream = File.Opentext(filetoread)

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

While streamer reads the content, we now need a place to store the contents of the file.  We will have to declare a variable named readcontents with data-type 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 the variable readcontents:

readcontents = fileStream.ReadToEnd()

Now we will have to declare a variable, which will store the delimiter.  Let us declare the variable named as textdelimiter with the data-type string.

Dim textdelimiter as String

Next, we’ll assign the delimiter, which may be a letter, word, number etc. For this example we’ll 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 = “ “

The value of the variable is mandatory and is very important, as the reading of the delimited file depends on the value that is stored in the variable.
 


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