开发者

exception while adding multiple rows to datatable. Couldn't store <System.Data.DataRowCollection>

开发者 https://www.devze.com 2023-01-19 01:50 出处:网络
idLocals is Primary key in database Populating the datatable oDtLocalCharge = bll.GetData(); i am copying the datatable in another function

idLocals is Primary key in database

Populating the datatable

oDtLocalCharge = bll.GetData();

i am copying the datatable in another function

   dtLocalCharges = oDtLocalCharge.Copy();

and trying to add rows using below code

DataRowCollection drr = oDtLocalCharge.Rows;
dtLocalCharges.Rows.Add(drr);

When i try to add rows to datatable, i am getting error as below


Error: 
{System.ArgumentException: Unable to cast object of type 'System.Data.DataRowCollection' to type 'System.IConvertible'.Couldn't store <System.Data.DataRowCollection> in idLocals Column.  Expected type is Int32. ---> System.InvalidCastException: Unable to cast object of type 'System.Data.DataRowCollection' to type 'System.IConvertible'.

May be Primary Key idLocals开发者_运维知识库is causing problem what is the problem?

How can i fix this? I want to multiple add rows in dtLocalCharges table


First off, you can't use Add() with a DataRowCollection. You need to add each row individually. You will probably need to use NewRow, copy the values and then call Add.

Try this first:

DataRowCollection drr = oDtLocalCharge.Rows;
foreach (var row in drr) dtLocalCharges.Rows.Add(row);


This is the best way to import all rows of one datatable into another datatable:

datatable1.Load(New DataTableReader(datatable2))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号