Hi,
I have one data table having datas from DB
tried but new coloumn started from 6th row of DT.5 rows are loaded from db.new coloumn started from 6th but 0th position
once the data table loaded i need to add one "Name" colum in the first position.My code is not giving a proper structure after adding new data column. What error i am doing here?
using (var adapter = new OdbcDataAdapter(command))
{
command.CommandTimeout = 280;
var table = new DataTable(tableName);
if (tableName == "TestData")
{
var pathC = @"H:\file\data\names.txt";
string[] result = File.ReadAllLines(pathC);
DataColumn Col = table.Columns.Add("Name", typeof(String));
Col.SetOrdinal(0); // set column to first position
int i = 0;
foreach (DataRow row in table.Rows)
{
//need to set value to NewColumn column
row["Name"] = result[i];
i++;
}
}
adapter.Fill(table);
return table;
}
Sujeet RamanPosted Dec 9, 2021, 5:08 PM
Above code is not working. I have used following code for an example but the data base fetched coloms are coming after 7 cells/rows.How i will fix this
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);
Sachin SinghPosted Dec 9, 2021, 4:18 PM
test
using (var adapter = new OdbcDataAdapter(command))
{
command.CommandTimeout = 280;
var table = new DataTable(tableName);
if (tableName == "TestData")
{
var pathC = @"H:\file\data\names.txt";
string[] result = File.ReadAllText(pathC ).Split('\n');
DataColumn Col = table.Columns.Add("Name", typeof(String));
Col.SetOrdinal(0); // set column to first position
int i = 0;
foreach (DataRow row in table.Rows)
{
//need to set value to NewColumn column
row["Name"] = result[i];
i++;
}
}
adapter.Fill(table);
return table;
}