In this article I will explain with an example, how to upload, read and display CSV file (Text File) data in ASP.Net GridView using C# and VB.Net.
CSV file is a Text file which contains Comma Separated (Comma Delimited) values. The data from a CSV file will be read and then after separating the values, a DataTable will be created which will be used to populate the ASP.Net GridView control.
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control, a Button and a GridView.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnImport" runat="server" Text="Import" OnClick="ImportCSV" />
<hr />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
VB.Net
Imports System.IO
Imports System.Data
Upload, Read and Display CSV file (Text file) data in ASP.Net GridView
When the Import Button is clicked, the CSV file is first uploaded and then saved inside a folder named Files.
The CSV file data is read into a String variable using the File class ReadAllText method.
A DataTable is created with columns same as that of the destination database table and then the CSV file data is split using New Line (\n) and Comma (,) characters and using a loop the data is saved into the DataTable.
Finally the DataTable is bound to the GridView control.
C#
protected void ImportCSV(object sender, EventArgs e)
{
//Upload and save the file
string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(csvPath);
//Create a DataTable.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
//Read the contents of CSV file.
string csvData = File.ReadAllText(csvPath);
//Execute a loop over the rows.
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
dt.Rows.Add();
int i = 0;
//Execute a loop over the columns.
foreach (string cell in row.Split(','))
{
dt.Rows[dt.Rows.Count - 1][i] = cell;
i++;
}
}
}
//Bind the DataTable.
GridView1.DataSource = dt;
GridView1.DataBind();
}
VB.Net
Protected Sub ImportCSV(sender As Object, e As EventArgs)
'Upload and save the file
Dim csvPath As String = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.SaveAs(csvPath)
'Create a DataTable.
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
'Read the contents of CSV file.
Dim csvData As String = File.ReadAllText(csvPath)
'Execute a loop over the rows.
For Each row As String In csvData.Split(ControlChars.Lf)
If Not String.IsNullOrEmpty(row) Then
dt.Rows.Add()
Dim i As Integer = 0
'Execute a loop over the columns.
For Each cell As String In row.Split(","c)
dt.Rows(dt.Rows.Count - 1)(i) = cell
i += 1
Next
End If
Next
'Bind the DataTable.
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Screenshots
The CSV File
GridView displaying CSV data
Downloads