简体   繁体   English

将行添加到数据集中的数据表

[英]Adding Row to datatable in dataset

I am trying to copy a row from one table to another in same dataset. 我正在尝试将一行从一个表复制到同一数据集中的另一个表。 code I am using 我正在使用的代码

dsFrom.Tables["asd2"].Rows.Add(dsFrom.Tables["asd"].Rows[0].ItemArray);

I am getting an NullRefferenceException. 我收到了NullRefferenceException。 I've determined that Rows are null, even though there is data in both tables. 我确定即使两个表中都有数据,行也为空。 Can anyone explain why is this happening? 谁能解释为什么会这样? Or maybe there is another solution for my problem. 也许有另一个解决方案可以解决我的问题。

Thanks 谢谢

EDIT 编辑
This is how I am loading data in them 这就是我在其中加载数据的方式

string query = @"select * from table1;
                SqlDataAdapter da = new SqlDataAdapter(query, conn);
                DataSet dsFrom = new DataSet();
                da.Fill(dsFrom, "asd");
                da.Fill(dsFrom, "asd2");

May be you should try to copy rows by this way: 可能是您应该尝试通过这种方式复制行:

foreach (var row in dsFrom.Tables["asd"].Rows)
{
     dsFrom.Tables["asd2"].ImportRow(row);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM