Click here to Skip to main content
15,298,916 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I have one data table having datas from DB

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?

What I have tried:

C#
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;

           }
Posted
Updated 9-Dec-21 22:40pm
v2
Comments
CHill60 9-Dec-21 9:21am
   
At quick glance I would say it's because you call adapter.Fill(table); after adding the column - try doing that first
Manidas Payyoli 9-Dec-21 9:32am
   
tried but new coloumn started from 6th row of DT.5 rows are loaded from db.new coloumn started from 6th nut 0th position
CHill60 9-Dec-21 9:56am
   
On which line
Manidas Payyoli 9-Dec-21 9:59am
   
index issue resolved.but new coloumn started from 6th row of DT.5 rows are loaded from db.new coloumn started from 6th nut 0th position
CHill60 9-Dec-21 10:02am
   
What index issue? I asked which line produced the exception!
Manidas Payyoli 9-Dec-21 10:17am
   
there is no exception, data loading as 0 th postion but after 5th row only newly added colom is loading.Till 5th row database values are loaded. i was getting an index issue befor that one i fixed.
CHill60 9-Dec-21 12:06pm
   
It might be easier if you actually included this data in your question. Do you mean the datatable ends up looking like this
Name1 database-data
Name2 database-data
Name3 database-data
Name4 database-data
Name5 database-data
Name6 blank
Name7 blank
Manidas Payyoli 9-Dec-21 12:45pm
   
NAME colom coming from txt file. empty cells left blank here.Header loding correctly from SCHOOL but data starts from last row

NAME SCHOOL PLACE
SAM
JAM
DAM
HAM SCHOOL1 PLACE1
Manidas Payyoli 9-Dec-21 12:45pm
   
this is the code

if (tableName == "TestData")

{

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

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

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

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

DataRow row;

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

{

row = table.NewRow();

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

table.Rows.Add(row);

}

}


adapter.Fill(table);
Manidas Payyoli 9-Dec-21 10:01am
   
tried diffrenet solution but no result
Richard Deeming 9-Dec-21 10:14am
   
That's not an excuse to keep re-posting the same question!

If you want to update your question with additional information, click the green "Improve question" link and edit your question. Do not post a "new" question with the updated information.
Manidas Payyoli 9-Dec-21 10:18am
   
thanks..ill care coming forwRD

Please look at my very first comment
Quote:
At quick glance I would say it's because you call adapter.Fill(table); after adding the column - try doing that first
Change your code to fill the table before you even attempt to add a column

Based on your new code in your comment you are now adding new rows to the table instead of filling in the new column details

Try something like this (warning, not tested or compiled):
C#
adapter.Fill(table);
if (tablename == "TestData")
{
	var pathC = @"H:\claimdetails\claims\names.txt";

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

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

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

    foreach (DataRow row in table.Rows)
    {
        row["Name"] = result[i++];
    }
}

// REMOVE this from here adapter.Fill(table);
   
v2
Comments
Manidas Payyoli 10-Dec-21 6:01am
   
db data starting after skipping first 5 rows. new column is in better positon but db data goes down
CHill60 10-Dec-21 6:04am
   
You are still using table.Rows.Add(row); aren't you?
You can try this,
using(var adapter = new OdbcAdapter(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;
foeeach (DataRow row in table.Rows)
{
row["Name"]=result[i];
i++;
}
}
adapter.Fill(table);
return table;
}
   
Comments
Manidas Payyoli 10-Dec-21 4:19am
   
only column name is loading..no data in first column
CHill60 10-Dec-21 4:30am
   
Reasons for my vote of 1:
- You have also put adapter.Fill(table); in the wrong place - it needs to be filled before adding the new column
- Pointless change of OdbcAdapter to OdbcDataAdapter - the OP is not having any problems reading their database
- Really pointless change to use string[]result = File.ReadAllText(pathC).Split('\n'); - Has the same effect as ReadAllLines and the latter is far more efficient
- Spot the problem with this line
if(tablename =="TestData");
- the statement block following this "if" statement will always be executed regardless of the tablename
- This has clearly not even been compiled never minded tested -
foeeach (DataRow row in table.Rows)
Manidas Payyoli 10-Dec-21 5:31am
   
if(tablename =="TestData");-- have number of tables in the the DS so only with this
table name i need to add extra column.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900