Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Solution of exception: This row already belongs to another table

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.57k
    Comment on it

    When you try to read row from one table and add to another table, if you get the exception i.e. ""This row already belongs to another table". For example:

    foreach (DataRow drWrite in dtRead.Rows)
    {
        dtWrite.Rows.Add(drWrite); //Error thrown here.
    }

    There are two solution for above exception:

    1. You need to create a "NewRow" with the values from reading row of reading table first. For example
      DataRow drWrite = dtWrite.NewRow();
      drWrite["Col1"] = dtRead.Rows[0]["Col1"];
      drWrite["Col2"] = dtRead.Rows[0]["Col2"];
      dtWrite.Rows.Add(drWrite);

       

    2. You can also use "Add" which takes an array of values. For example

      dtWrite.Rows.Add(drWrite.ItemArray);

      or

      dtWrite.ImportRow(drWrite);

       

    Hope it helps. Happy Coding!!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: