Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to read and display text file data in ASP.NET

Author: Rudy Pinson
Download Source Code :

I will show you how you can read and display text file data in ASP.NET.


You can read text files using StreamReader class. StreamReader class has Read(), ReadBlock(), ReadLine() and ReadToEnd() methods. First two methods read maximum characters from the current stream with given length from starting index. ReadLine() method reads a line of characters and return data as string. ReadToEnd() method reads stream from the current position to the end of stream.

  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Add a Text File to Web Site and write few lines of text in it
  4. Drag and drop three TextBox controls and a ListBox control in aspx page

    <asp:TextBox ID="TextBox1" runat="server" Height="120px" TextMode="MultiLine"
    <br />
    <asp:TextBox ID="TextBox2" runat="server" Height="120px" TextMode="MultiLine"
    <br />
    <asp:TextBox ID="TextBox3" runat="server" Height="120px" TextMode="MultiLine"
     <br />
     <asp:ListBox ID="ListBox1" runat="server" Height="120px" Width="100px"></asp:ListBox>
  5. Open code behind file and include below namespaces in code behind file


    using System.IO;
    using System.Text;
    using System.Collections;

    Imports System.IO
    Imports System.Text
    Imports System.Collections
  6. Write code below in Page Load event

    protected void Page_Load(object sender, EventArgs e)
        string filePath = Server.MapPath("TextFile.txt");
        StreamReader reader1 = new StreamReader(filePath);
        StreamReader reader2 = new StreamReader(filePath);
        StreamReader reader3 = new StreamReader(filePath);
        StreamReader reader4 = new StreamReader(filePath);
        //ReadToEnd() method
        string text1 = reader1.ReadToEnd();
        TextBox1.Text = text1;
        //ReadBlock() method
        char[] textBlock = new char[65];
        reader2.ReadBlock(textBlock, 0, textBlock.Length);
        StringBuilder builder1 = new StringBuilder();
        for (int i = 0; i < textBlock.Length; i++)
        TextBox2.Text = builder1.ToString();
        //Read() and Peak() methods
        char[] text2 = new char[65];
        while (reader3.Peek() >= 0)
            reader3.Read(text2, 0, text2.Length);
        StringBuilder builder2 = new StringBuilder();
        for (int i = 0; i < text2.Length; i++)
        TextBox3.Text = builder2.ToString();
        //ReadLine() method
        ArrayList list = new ArrayList();
        while (!reader4.EndOfStream)
            string text3 = reader4.ReadLine();
        ListBox1.DataSource = list;
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim filePath As String = Server.MapPath("TextFile.txt")
        Dim reader1 As New StreamReader(filePath)
        Dim reader2 As New StreamReader(filePath)
        Dim reader3 As New StreamReader(filePath)
        Dim reader4 As New StreamReader(filePath)
       'ReadToEnd() method
        Dim text1 As String = reader1.ReadToEnd()
        TextBox1.Text = text1
        'ReadBlock() method
        Dim textBlock As Char() = New Char(64) {}
        reader2.ReadBlock(textBlock, 0, textBlock.Length)
        Dim builder1 As New StringBuilder()
        For i As Integer = 0 To textBlock.Length - 1
        TextBox2.Text = builder1.ToString()
        'Read() and Peak() methods
        Dim text2 As Char() = New Char(64) {}
        While reader3.Peek() >= 0
            reader3.Read(text2, 0, text2.Length)
        End While
        Dim builder2 As New StringBuilder()
        For i As Integer = 0 To text2.Length - 1
        TextBox3.Text = builder2.ToString()
        'ReadLine() method
        Dim list As New ArrayList()
        While Not reader4.EndOfStream
            Dim text3 As String = reader4.ReadLine()
        End While
        ListBox1.DataSource = list
    End Sub
    First I have created four StreamReader objects for text file. ReadToEnd() method reads stream from current position to the end of the file. I have assigned text file data to string variable and displayed it in TextBox control.
     I have created a char array to read data from text file using ReadBlock() method. ReadBlock() method takes three parameters, char buffer, starting index and number of characters to read. It returns characters and I have converted these characters into string using StringBuilder class and displayed this string in a TextBox control.
    Read() method reads the next character and advances the position to one character. The overloaded of Read() method do the same as ReadBlock and takes same parameters.  I have used Peak() method in While Loop condition. The Peak() method returns the next character but do not use it. The Read() method will read characters until the Peak() method returns last character. I have used same StringBuilder class to build string and then displayed it in a TextBox control.
    I have used ArrayList object to demonstrate the ReadLine() method. In While Loop condition, I have used EndofStream property to check whether the current stream position is at the end. ReadLine() method reads a line of character and returns a string. I have added each line to ArrayList object and bound this ArrayList object to ListBox control.
  7. Now View the Web Site in browser
Add Article Comment:
Name :
Email Address :
Comments :
<< How to add two or more SQL Server database tables in DataSet in ASP.NET

Disclaimer - Privacy
© 2002-2017