如何解决这个错误:“名为'Rowno'的列已经属于这个DataTable" [英] how to solve this erro : "A column named 'Rowno' already belongs to this DataTable"

查看:418
本文介绍了如何解决这个错误:“名为'Rowno'的列已经属于这个DataTable"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在合并foreach中的数据表中的值。 foreach使用id迭代工作。 id = 1,2,3,4



hi i am merging value in datatable inside foreach. foreach works using id iteration. id = 1,2,3,4

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


}



这里第一次迭代正常工作。



当它进入第二次迭代时我收到此错误:名为'Rowno'的列已经属于此DataTable。



我知道为什么我会收到这个错误。而第一次迭代rowno被命名为名称。所以当第二次迭代去的时候,这行chktb1.Columns.Add(column);抛出该错误,因为该名称已存在。我试图尽可能地解决。但我仍然遇到问题。如何解决这个问题。需要帮助。


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.

推荐答案

停下来思考。

如果你结婚,生了孩子,你可以称之为克里斯你自己。这可能有点令人困惑,但是年龄差异不应该是一个主要问题。



如果你有双胞胎或三胞胎怎么办?你不能称他们为克里斯,因为这会令人困惑 - 他们不会知道你或他们的母亲或他们的老师在说什么,其他人也不会。



相反,你可以称它们为Chris1,Chris2等等 - 如果你需要在每次循环时向表中添加一列,那么这就是你需要做的。



我对你希望代码做什么感到有点困惑:每次循环,你创建两个新表,用相同的行加载它们来自同一个数据库表(我怀疑它是空的或最多包含一行) 然后你将它们扔掉然后绕回循环。



所以忽略你遇到的问题 - 并想一想你为什么做一些实际上无能为力的事情!



和BTW:你需要处理你的SqlConnections ......
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...


试试这个..



try this..

if (!chktb1.Columns.Contains("Rowno"))
                chktb1.Columns.Add(column);


这篇关于如何解决这个错误:“名为'Rowno'的列已经属于这个DataTable"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆