How To Read CSV File In ASP.NET With Example C# and VB.NET

Introduction

In this article, I'll show you, with an example, a way to upload, read/browse and show CSV file (Text File) information in ASP.NET GridView using C# and VB.NET.

CSV file is a computer file that contains Comma Separated (Comma Delimited) Values. The information from a CSV file is browsed and then once the values are separated, a DataTable is created which is able to populate the ASP.NET GridView control.

HTML Markup

The following HTML Markup consists of an associate ASP.NET FileUpload control, a Button, and a GridView.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         body  
  9.         {  
  10.             font-family: Arial;  
  11.             font-size: 10pt;  
  12.         }  
  13.         table  
  14.         {  
  15.             border: 1px solid #ccc;  
  16.             border-collapse: collapse;  
  17.             background-color: #fff;  
  18.         }  
  19.         table th  
  20.         {  
  21.             background-color: #ff7f00;  
  22.             color: #fff;  
  23.             font-weight: bold;  
  24.         }  
  25.         table th, table td  
  26.         {  
  27.             padding: 5px;  
  28.             border: 1px solid #ccc;  
  29.         }  
  30.         table, table table td  
  31.         {  
  32.             border: 0px solid #ccc;  
  33.         }  
  34.         .button {  
  35.     background-color: #0094ff; /* Blue */  
  36.     border: none;  
  37.     color: white;  
  38.     padding: 15px 32px;  
  39.     text-align: center;  
  40.     text-decoration: none;  
  41.     display: inline-block;  
  42.     font-size: 16px;  
  43. }  
  44.     </style>  
  45. </head>  
  46. <body>  
  47.     <form id="form1" runat="server">  
  48.     <asp:FileUpload ID="FileUpload1"  CssClass="button"  runat="server" />  
  49.     <asp:Button ID="btnImport" CssClass="button" runat="server" Text="Import" OnClick="ImportCSV" />  
  50.     <hr />  
  51.     <asp:GridView ID="GridView1" runat="server">  
  52.     </asp:GridView>  
  53.     </form>  
  54. </body>  
  55. </html>  

Namespaces

For reading the text from CSV file, you need to import the following namespace.

C#

  1. using System.IO;  
  2. using System.Data;  

VB.Net

  1. Imports System.IO  
  2. Imports System.Data  

Upload, Read/browse and show CSV file (Text file) information in ASP.Net GridView

When the Import button is clicked, the CSV file is first uploaded and then saved within a folder named Files. The CSV file information is browsed into a String variable using the File class ReadAllText method.

A DataTable is formed with columns same as that of the destination database table so the CSV file information is split using the new line (\n) and Comma (,) characters. Employing a loop, the information is saved into the DataTable.

Finally, the DataTable is certain to the GridView control.

C#

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8. using System.Data;  
  9.   
  10. public partial class CS : System.Web.UI.Page  
  11. {  
  12.     protected void ImportCSV(object sender, EventArgs e)  
  13.     {  
  14.         //Upload and save the file  
  15.         string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);  
  16.         FileUpload1.SaveAs(csvPath);  
  17.   
  18.         //Create a DataTable.  
  19.         DataTable dt = new DataTable();  
  20.         dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Id"typeof(int)),  
  21.         new DataColumn("Name"typeof(string)),  
  22.         new DataColumn("Technology"typeof(string)),  
  23.         new DataColumn("Company"typeof(string)),  
  24.         new DataColumn("Country",typeof(string)) });  
  25.   
  26.         //Read the contents of CSV file.  
  27.         string csvData = File.ReadAllText(csvPath);  
  28.   
  29.         //Execute a loop over the rows.  
  30.         foreach (string row in csvData.Split('\n'))  
  31.         {  
  32.             if (!string.IsNullOrEmpty(row))  
  33.             {  
  34.                 dt.Rows.Add();  
  35.                 int i = 0;  
  36.   
  37.                 //Execute a loop over the columns.  
  38.                 foreach (string cell in row.Split(','))  
  39.                 {  
  40.                     dt.Rows[dt.Rows.Count - 1][i] = cell;  
  41.                     i++;  
  42.                 }  
  43.             }  
  44.         }  
  45.   
  46.         //Bind the DataTable.  
  47.         GridView1.DataSource = dt;  
  48.         GridView1.DataBind();  
  49.     }  
  50. }  

VB.Net

  1. Imports System.IO  
  2. Imports System.Data  
  3.   
  4. Partial Class VB  
  5.     Inherits System.Web.UI.Page  
  6.     Protected Sub ImportCSV(sender As Object, e As EventArgs)  
  7.         'Upload and save the file  
  8.         Dim csvPath As String = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName)  
  9.         FileUpload1.SaveAs(csvPath)  
  10.   
  11.         'Create a DataTable.  
  12.         Dim dt As New DataTable()  
  13.         dt.Columns.AddRange(New DataColumn(4) {New DataColumn("Id"GetType(Integer)), New DataColumn("Name"GetType(String)), New DataColumn("Technology"GetType(String)), New DataColumn("Company"GetType(String)), New DataColumn("Country"GetType(String))})  
  14.   
  15.         'Read the contents of CSV file.  
  16.         Dim csvData As String = File.ReadAllText(csvPath)  
  17.   
  18.         'Execute a loop over the rows.  
  19.         For Each row As String In csvData.Split(ControlChars.Lf)  
  20.             If Not String.IsNullOrEmpty(row) Then  
  21.                 dt.Rows.Add()  
  22.                 Dim i As Integer = 0  
  23.   
  24.                 'Execute a loop over the columns.  
  25.                 For Each cell As String In row.Split(","c)  
  26.                     dt.Rows(dt.Rows.Count - 1)(i) = cell  
  27.                     i += 1  
  28.                 Next  
  29.             End If  
  30.         Next  
  31.   
  32.         'Bind the DataTable.  
  33.         GridView1.DataSource = dt  
  34.         GridView1.DataBind()  
  35.     End Sub  
  36. End Class  

Screenshots

CSV File

CSV File

Output

Output

Summary

CSV file is a computer file that contains Comma Separated (Comma Delimited) values. The information from a CSV file is scanned and then, separating the values, a DataTable is created which is able to be accustomed populate the ASP.Net GridView control.