Tips Station Asp.net Article Seo Articles
Tutorials Code Samples
›  Home
›  Mission
›  About us
›  Contact Us
›  Feedback
›  Terms & Condition
Asp Articles
IT Solutions
 
› ASP.NET

› Programming Tips

› Ajax

› Asp

› ADO.NET

› Databases

› SEO

› CSS And Designing

› Php

 
Most Viewed Articles
 
› Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

› Change theme dynamically without page refresh in ASP.NET

› Install AJAX On Machines Running Visual Studio 2005

› Creating Pretty Popups Using AJAX

› Simple ASP Image Resize Function

› SQL Server Performance Counters

› ASP.Net Interview Questions And Their Answers

› Encode Url using ASP

› Difference Between DataGrid and GridView in Asp.Net

› Select Specific Value WithIn Drop Down List Or Radio Button List

more...
 
 
Home> ASP.NET
 
Read and display a text file in Asp.net
 
An example of how we can read a text file and bind it's contents to a GridView control

There may be occasions when we would like to display the contents of a flat file (such as a text file). To do so, we can make use of the System.IO.StreamReader object.

First, let's create a simple text file (in this case I've named it TextFile.txt):
  1. This is line 1  
  2. This is line 2  
  3. This is line 3  
  4. This is line 4  
  5. This is line 5  

Then, we'll create a page with just a GridView on it that we'll use to display the contents of this file:
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="DisplayTextFile.aspx.vb" Inherits="DisplayTextFile" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7.     <title>Untitled Page</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:GridView ID="GridView1" runat="server">  
  13.         </asp:GridView>  
  14.     </div>  
  15.     </form>  
  16. </body>  
  17. </html>  

Next, we need to create an instance of the StreamReader object and point it at the text file. You'll notice that I've used the Server.MapPath method to get a full path to my text file and I've used this method as I placed the file in the root directory of my application, so if you haven't done the same, you'll have to amend the path. We then need to loop through the file using the Peek method and add each line to an object that can be bound to the GridView (in this case I've used an ArrayList). We can then bind this ArrayList directly to the GridView:
  1. Imports System.IO  
  2.   
  3. Partial Class DisplayTextFile  
  4.     Inherits System.Web.UI.Page  
  5.   
  6.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
  7.         ' Declarations  
  8.         Dim objStreamReader As New StreamReader(Server.MapPath("TextFile.txt"))  
  9.         Dim arrText As New ArrayList  
  10.   
  11.         ' Loop through the file and add each line to the ArrayList  
  12.         Do While objStreamReader.Peek() >= 0  
  13.             arrText.Add(objStreamReader.ReadLine)  
  14.         Loop  
  15.   
  16.         ' Close the reader  
  17.         objStreamReader.Close()  
  18.   
  19.         ' Bind the results to the GridView  
  20.         GridView1.DataSource = arrText  
  21.         GridView1.DataBind()  
  22.     End Sub  
  23. End Class  

This shows the method in the simplest form I could think of, so feel free to improve on this and apply whatever formatting necessary to show the file contents in a well presented manner.

 
 
Vrp Technologies
 
Serversea Hosting
 
 
Latest Articles
 
› Sending SMS With PHP

› MySQL Join Tutorial

› Make An RSS Feed Using PHP

› Intro To Object: Option Variables

› Design An Online Chat Room With PHP And MySQL

› Create Tell A Friend Script With HTML & PHP

› Benchmark And Optimize PHP Script Speed

› What Kind of DBA Are You?

› SQL Server Performance Counters

› SQL Server Performance Tips

more...
 
Random Articles
 
› How to connect to a database via a DSN-Less connection - ASP

› MySQL Join Tutorial

› Filter Certain Words in a String - Asp.net

› Understanding DDL Triggers In SQL Server 2005

› Displaying Data using ASP.NET 2.0 DataList

› How To Set Up A 301 Redirect On IIS

› Cache Data within your Application - Asp.Net

› Creating Tableless Sites - Why And Some Basics

› SubRoutines vs. Functions - ASP

› Successful CSS Template Skins

more...
 
Home Mission About us Contact us Feedback Terms Conditions
2008 © Copyright TipsStation. All rights reserved.