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

Discussion in 'ASP .Net' started by Ersin Gençtürk, Oct 5, 2004.

  1. 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
     
    Ersin Gençtürk, Oct 5, 2004
    #1
    1. Advertising

  2. Uzytkownik "Ersin Gençtürk" <ers[spam]@gencturk.org> napisal w wiadomosci
    news:uFKnj%...
    >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" <ers[spam]@gencturk.org> wrote in message
    news:...
    > 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
    >
    >
    >
    >
    >
    >
     
    Ersin Gençtürk, Oct 6, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Babe
    Replies:
    1
    Views:
    8,380
    Kevin Spencer
    Aug 15, 2003
  2. MattB

    add row from dataview to DataTable

    MattB, Nov 9, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    9,549
    MattB
    Nov 10, 2005
  3. Paul
    Replies:
    1
    Views:
    2,336
    Hermit Dave
    Jul 11, 2008
  4. Andrew Jocelyn
    Replies:
    2
    Views:
    1,541
    Allen Chen [MSFT]
    Apr 27, 2009
  5. D
    Replies:
    0
    Views:
    230
Loading...

Share This Page