Maybe it's a bad day or I am a stupid. I was trying to create a data table from a file upload (.txt).
10|00309|IN3136|EKM|110|13-12-2019|V1.1|||||
20|1|01|02|13122019120157_1||Please Enter Mother's First Name|
20|2|01|02|13122019120157_2||Please Enter Mother's First Name|
20|3|01|02|13122019120157_3||Please Enter Mother's First Name|
is the data inside the text file.
var data = File.ReadAllText(FilePath);
string[] strArr = null;
int count = 0;
char[] splitchar = { '|' };
strArr = data.Split(splitchar);
DataTable dt2 = new DataTable();
dt2.Clear();
dt2.Columns.Add("Col0");
dt2.Columns.Add("Col1");
dt2.Columns.Add("Col2");
dt2.Columns.Add("Col3");
dt2.Columns.Add("Col4");
dt2.Columns.Add("Col5");
dt2.Columns.Add("Col6");
for (count = 0; count <= strArr.Length - 1; count++)
{
if (count >= 11)
{
DataRow _dr2 = dt2.NewRow();
for (int i = 0; i <= 6; i++)
{
if (i == 0)
_dr2["Col0"] = strArr[count+i];
else if (i == 1)
_dr2["Col1"] = strArr[count+i];
else if (i == 2)
_dr2["Col2"] = strArr[count+i];
else if (i == 3)
_dr2["Col3"] = strArr[count+i];
else if (i == 4)
_dr2["Col4"] = strArr[count+i];
else if (i == 5)
_dr2["Col5"] = strArr[count+i];
else if (i == 6)
_dr2["Col6"] = strArr[count+i];
}
dt2.Rows.Add(_dr2);
}
}
I am trying to make it like this
my loops are completely stupid, I know, please point me in the right direction..


ifstatements and just have_dr2[$"Col{i}"] = strArr[count+i];, this uses string interpolation and gets rid of the bug you've introduced on the last one :)string[]for each line in file. However reading all file in memory can take all memory for large files so you can check the StreamReader.ReadLine() to process file line by line. Then you can split each line by|and create table row