Sujeet Raman

Sujeet Raman

  • 809
  • 915
  • 336.2k

How to add a column and values in to existing Data table from a .txt

Dec 10 2021 6:30 AM

I have tried multiple solutions and ended up here and blocked.I have to add a data column in to the existing data table.

I need to add one more column and data to that table from a .txt file in to 0th column and from second column i need to keep the DB data. I am not using any grid view and stream reader to my app.Need to use File.ReadAllLines();

my .txt file is like

ClaimID
val1      
val3      
val5

i tried something and the data table not coming properly..Data table coming like showing in the picture below (datas starting from last row of first column and skipped first rows):

 

My code

 

if (tableName == "TestData")

{

var pathC = @"H:\claimdetails\claims\Claims.txt";

string[] result = File.ReadAllLines(pathC);

DataColumn Col = table.Columns.Add("Claim_ID", typeof(String));

Col.SetOrdinal(0); // set column to first position

DataRow row;

for (int i = 0; i < result.Length; i++)

{

row = table.NewRow();

row["Claim_ID"] = result[i];

table.Rows.Add(row);

}

}

adapter.Fill(table);

How can i get a proper data table?


Answers (4)