Click here to Skip to main content
15,890,438 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
[no name] 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
[no name] 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!

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);
 
Share this answer
 
v2
Comments
[no name] 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;
}
 
Share this answer
 
Comments
[no name] 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)
[no name] 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
Top Experts
Last 24hrsThis month


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