views:

31

answers:

1

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 idLocalsis causing problem what is the problem?

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

A: 

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);
Ray Henry
datatable.ImportRow(OtherDataTable) did the work for me!
Aamod Thakur
Good one! I didn't look at the entire DataTable API.
Ray Henry