User-271186128 posted
Hi Ravikumar,
As for this issue, I suggest you could try to use the
StreamReader.ReadLine Method to read the text files.You could refer to the following code:
The text file:
id|name|age|city|state|country
1|ravi|28|Bangalore|karnataka|India
2|kumar|30|Bangalore|karnataka|India
Code:
//According to the first line in the text file, create a dataTable
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6] { new DataColumn("ID"), new DataColumn("Name"), new DataColumn("Age"), new DataColumn("City"), new DataColumn("State"), new DataColumn("Country") });
StringBuilder sb = new StringBuilder();
List<string> list = new List<string>();
using (StreamReader sr = new StreamReader(@"D:\MyFolder\Test.txt"))
{
while (sr.Peek() >= 0)
{
list.Add(sr.ReadLine()); //Using readline method to read text file.
}
}
//ignore the fist line (title line).
for (int i = 1; i < list.Count;i++ )
{
string[] strlist = list[i].Split('|'); //using string.split() method to split the string.
dt.Rows.Add(strlist[0], strlist[1], strlist[2], strlist[3], strlist[4], strlist[5]);
//If you want to insert it into database, you could insert from here.
}
GridView1.DataSource = dt;
GridView1.DataBind();
The output screenshot:

Best Regards,
Dillion