ASP.NET
  Home arrow ASP.NET arrow Page 3 - Reading a Delimited File Using ASP.Net and...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Reading a Delimited File Using ASP.Net and VB.Net
By: G Ajaykumar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 96
    2003-10-15

    Table of Contents:
  • Reading a Delimited File Using ASP.Net and VB.Net
  • Usage
  • Example Code
  • Example Code, Cont'd
  • Code Explanation
  • Output

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.
     

    More ASP.NET Articles
    More By G Ajaykumar


       · hi!nice code.i have a little problem as i m using ur code but if my .txt file...
       · This was an excellent article. I found it very logically laid out and clearly...
     

    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







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway