Introduction
NOTE
For source code, you can download it from codingvila.com or you can mail info.codingvila@gmail.com.
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. If you want to export datatable to CSV file you can read this article, i not re-posting the same article on c# corner to avoid duplicate content.
HTML Markup
The following HTML Markup consists of an associate ASP.NET FileUpload control, a Button, and a GridView.
Namespaces
For reading the text from CSV file, you need to import the following namespace.
C#
VB.Net
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#
VB.Net
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.
Reference Link