2

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

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Good one! I didn't look at the entire DataTable API.
1

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

datatable1.Load(New DataTableReader(datatable2))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.