How can I add a row from a typed datatable to another instance of that typed datatable?

  • Thread starter Ersin Gençtürk
  • Start date
E

Ersin Gençtürk

I have 2 typed data tables inherited from the same dataset schema
One called : table A with an identity column x
column x is constrained to be unique.
Other one is : table B with an identity column x

There are some rows in table A and table B.
When I want to add a row from table A to table B I use the function

TableA.ImportRow(TableB.rows[1]); // for example

If the column x values are the same I get the error :

Column 'column x' is constrained to be unique. Value '34' is already
present.

I want TableA to continue increasing values from the greatest value in Table
B

I tried making column A's values to null but it didn't worked.

TableA.SetColumnXNull();

How can I add a row from a typed datatable to another instance of that typed
datatable with an auto incremeant and uniqe column.

thanks ,
ersin
 
E

Ersin Gençtürk

Uzytkownik "Ersin Gençtürk said:
I have 2 typed data tables inherited from the same dataset schema
One called : table A with an identity column x
column x is constrained to be unique.
Other one is : table B with an identity column x

There are some rows in table A and table B.
When I want to add a row from table A to table B I use the function

TableA.ImportRow(TableB.rows[1]); // for example

If the column x values are the same I get the error :

Column 'column x' is constrained to be unique. Value '34' is already
present.

I want TableA to continue increasing values from the greatest value in
Table

I'm not sure if there is any simple method to do it. I propose that
solution:

public void AddR()
{
foreach(DataRow dr in tableA.Rows)
ImportOneRow(tableB, dr);
}


private void ImportOneRow(DataTable targetTable, DataRow sourceRow)
{
//"Id" - name of identity column
int curId = (int)sourceRow["Id"];
//temporary changing value of identity column
sourceRow["Id"] = -1;

//importing new row
targetTable.ImportRow(sourceRow);

//restoring previous value of identity column
sourceRow["Id"] = curId;

//computing current value of identity column
object ob = targetTable.Compute("Max(Id)", "Id <> -1");
int id;
if(Convert.IsDBNull(ob))
id = 1;
else
id = (int)ob + 1;

//assigning proper value of identity column in target table
DataRow drTarg = targetTable.Rows.Find(-1);
drTarg["Id"] = id;
}

I hope that it helps.

Regards,
Grzegorz

Ersin Gençtürk said:
I have 2 typed data tables inherited from the same dataset schema
One called : table A with an identity column x
column x is constrained to be unique.
Other one is : table B with an identity column x

There are some rows in table A and table B.
When I want to add a row from table A to table B I use the function

TableA.ImportRow(TableB.rows[1]); // for example

If the column x values are the same I get the error :

Column 'column x' is constrained to be unique. Value '34' is already
present.

I want TableA to continue increasing values from the greatest value in Table
B

I tried making column A's values to null but it didn't worked.

TableA.SetColumnXNull();

How can I add a row from a typed datatable to another instance of that typed
datatable with an auto incremeant and uniqe column.

thanks ,
ersin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top