2
Answers

Why Data column is not adding properly to the datatable

Sujeet Raman

Sujeet Raman

3y
823
1

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;
}

 

Answers (2)