How to Read Data from a CSV File in Asp.Net using StreamReader Class in C# and VB.Net

You can read a CSV file in Asp.Net. Its very simple. Here I am sharing a simple and small example on how to read data from a CSV file in Asp.Net using the StreamReader Class. I have written the code in both C# and VB.Net.

Ref: You can read more about the StreamReader Class here.

The StreamReader Class provides many useful methods and properties to read lines of information from a standard text file.

The CSV (or Comma Separated Value) data in my example here is stored in a .txt file. I have a list of birds in the file, each line (of data) is separated by a comma. Using StreamReader class methods, I can easily extract the CSV data and write it on my web page for viewing.

The Markup

In the markup section, I have <div> element, where I’ll write the CSV data. Its optional. You can use other ways to deal with the data, like saving it to a database etc.

<style>
    .header, .d {
        display:inline-table;
        width:30%;
        border:solid 1px #CCC;
        padding:3px;
        font:13px Verdana;
    }
    .header {
        font-weight:bold;
    }
</style>

<div id="birds" runat="server" style="width:50%;"></div>
The Code Behind Procedure (C#)
using System;
using System.IO;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // BE CAREFULL WITH Backward and forward SLASH IN FILE PATH.
        readCSV_Data("d:/birds.txt");
    }

    private void readCSV_Data(string sfileName)
    {
        // CREATE AN INSTANCE OF StreamReader TO READ THE FILE.
        using (StreamReader sr = new StreamReader(sfileName))
        {
            // THE HEADER.
            birds.InnerHtml = "<div class='header'>Code</div><div class='header'>Bird Name</div><div class='header'>Bird Type</div>";

            do
            {
                string s = sr.ReadLine();           // READ LINE OF CHARACTERS FROM THE STREAM.
                string[] arrData = s.Split(',');    // STORE VALUES IN THE ARRAY.

                // READ VALUES FROM THE ARRAY.
                for (int i = 0; i <= arrData.GetUpperBound(0); i++)
                {
                    birds.InnerHtml = birds.InnerHtml + "<div class='d'>" + arrData[i] + "</div>";
                }

                birds.InnerHtml = birds.InnerHtml + "<br />";       // GO TO THE NEXT LINE.

            } while (!(sr.EndOfStream));
        }
    }
}

I am creating an Instance of StreamReader to read the data from the file. Using the ReadLine() method, I am storing each character in a string.

Code Behind (Vb.Net)
Option Explicit On
Imports System.IO

Partial Class Site
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        readCSV_Data("d:/birds.txt")
    End Sub

    Private Sub readCSV_Data(ByVal sfileName As String)
        ' CREATE AN INSTANCE OF StreamReader TO READ THE FILE.
        Using sr As New StreamReader(sfileName)
            
            ' THE HEADER.
            birds.InnerHtml = "<div class='header'>Code</div><div class='header'>Bird Name</div><div class='header'>Bird Type</div>"
                
            Do
                Dim arrData() As String = sr.ReadLine.Split(",")	' SPLIT AND STORE VALUES IN THE ARRAY.

                ' READ VALUES FROM THE ARRAY.
                For i = 0 To UBound(arrData)
                    birds.InnerHtml = birds.InnerHtml & "<div class='d'>" & arrData(i) & "</div>"
                Next

                birds.InnerHtml = birds.InnerHtml & "<br />"  ' GO TO THE NEXT LINE.

            Loop Until sr.EndOfStream
        End Using
    End Sub
End Class

Well, That’s it. Thanks for reading.

Previous - How to Populate a SELECT element with Data from SQL Server using Asp.Net C#Next - Convert CSV to XML in Asp.Net using StreamReader and DataTable in C# and VB



Like this Article? Subscribe now, and get all the latest articles and tips, right in your inbox.

Enter your email id

Delivered by FeedBurner
Tweet this article Google+

Related Posts:

Join our Google Plus Community and be a part of a discussion!