Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi i am merging value in datatable inside foreach. foreach works using id iteration. id = 1,2,3,4

C#
foreach (ListItem list in chek.Items)
{

qlConnection pubconncheck = new SqlConnection(connstr);

SqlCommand cmd4 = new SqlCommand("select * from table where id = id_value");


SqlDataAdapter sp_adap = new SqlDataAdapter(cmd4);

DataTable chktb1 = new DataTable();
DataTable sp_dt = new DataTable();
sp_adap.Fill(sp_dt);
DataColumn column = new DataColumn("Rowno");
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;


chktb1.Columns.Add(column);

chktb1.Merge(sp_dt);


}

here first iteration works correctly.

when it goes to second iteration i am getting this error : "A column named 'Rowno' already belongs to this DataTable".

I know why i am getting this error. while first iteration rowno is assinged as name. so when the second iteration go, this line "chktb1.Columns.Add(column);" throws that error as already the name was existing. i tried to solve as much as possible. but still i am getting problem. how to solve that problem. help needed.
Posted
Updated 22-Nov-13 7:21am
v2
Comments
Richard MacCutchan 22-Nov-13 13:15pm    
Seriously? Take the column creation out of the loop.
Asp_Learner 22-Nov-13 13:29pm    
look at your code ,you are creating columns with single name , try to change name in every iteration or

Stop and think.
If you marry, and have a child, you might call it "Chris", after yourself. It might be a little confusing, but with the age difference it shouldn't be a major problem.

What if you had twins, or triplets? You can't call them all "Chris" because that would be confusing - they wouldn't know who you, or their mother, or their teachers were talking about, and neither would anybody else.

Instead, you might call them "Chris1", "Chris2" and so on - and if you need to add a column to the table each time round the loop, then that is what you need to do.

I'm a little confused as to what you expect that code to do: Each time round the loop, you create two new tables, load them with the same rows from the same database table (which I suspect is empty or contains at most one row) and then you throw them away and go back round the loop.

So ignore the problem you are getting - and think about why you are doing something that in practical terms does nothing anyway!

And BTW: You need to Dispose your SqlConnections...
 
Share this answer
 
Comments
Ron Beyer 22-Nov-13 16:22pm    
Even George Forman knows to name his kids with a 1,2,3, etc at the end! :)
christhuxavier 22-Nov-13 23:36pm    
Thank you. i understood what you example story imparts. i am not binding two tables from database. i can take row number from database using this query below:

SELECT ROW_NUMBER() OVER (ORDER BY ColumnA DESC) AS RowNo,
ColumnA,
ColumnB,
ColumnC
FROM YourTable
but, when you do first iteration. it comes correctly. the first time result would be like,
rowno name
1 chris
2 Ram
when it finishes second iteration , the result will be below as i am merging into one table:
rowno name
1 chris
2 Ram
1 avanash //see again it starts from 1. it is supposed to start 3.
2 karthick.

few people suggest use key for two table. that is not possible. becuase for example i mentioned two table. like that i am merging morethan 10 table' records in one datatable. that is why i made that Rowno is not coming from database. because i need to build rowno from front end like adding column as rowno.
try this..

C#
if (!chktb1.Columns.Contains("Rowno"))
                chktb1.Columns.Add(column);
 
Share this answer
 
Comments
christhuxavier 23-Nov-13 0:07am    
hi Karthik, Thanks a lot. Excellent..your coding works for me. i rated 5 for you. Thank you so much.
Karthik_Mahalingam 23-Nov-13 0:41am    
always welcome..
happy coding :)

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



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