Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:05:08+00:00 2026-06-11T17:05:08+00:00

I get an error ‘this row already belongs to this table’ from the following

  • 0

I get an error ‘this row already belongs to this table’ from the following code:

public static DataTable AddNewAllocations(string pCaseNo, ref DataTable pTable)
    {
        try
        {
            string sqlText = "SELECT UserID FROM tblUsers;";
            aSqlQuery aQ = new aSqlQuery(sqlText, "table");
            DataTable userTable = aQ.TableResult;

            foreach (DataRow userRow in userTable.Rows)
            {
                int allocAlready = 0;
                foreach (DataRow allocRow in pTable.Rows)
                {
                    if (allocRow["FeeEarner"].ToString() == userRow["UserID"].ToString())
                    {
                        allocAlready = 1;                            
                    }
                }
                if (allocAlready == 0)
                {
                    string strUser = userRow["UserID"].ToString();          
                    decimal fees = cTimesheet.UserFees(strUser, pCaseNo);
                    int intCaseNo = Int32.Parse(pCaseNo);
                    if (fees > 0)
                    {
                        Object[] array = new object[8];
                        array[0] = 0;
                        array[1] = intCaseNo;
                        array[2] = DateTime.Today;
                        array[3] = strUser;
                        array[4] = fees;
                        array[5] = 0;
                        array[6] = fees;
                        array[7] = true;
                        pTable.Rows.Add(array);
                    }
                }
            }
            return pTable;
        }

        catch (Exception eX)
        {
            throw new Exception("cAllocation: Error in NewAllocations()" + Environment.NewLine + eX.Message);
        }

When I step through the code I can see that the error is thrown on the second occasion the following line is accessed:

pTable.Rows.Add(array);

Given that I create a new object array each time the code enters the loop, I fail to see why I am receiving this error message, which suggests to me the same row is being added multiple times. Why does the code see each loop as adding the same datarow when the row is generated by a new object array each time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:05:09+00:00Added an answer on June 11, 2026 at 5:05 pm

    The code that finally worked was this:

    public static DataTable AddNewAllocations(string pCaseNo, DataTable pTable)
        {
            try
            {
                DataTable newTable = NewAllocationTable(); 
    
                string sqlText = "SELECT UserID FROM tblUsers;";
                aSqlQuery aQ = new aSqlQuery(sqlText, "table");
                DataTable userTable = aQ.TableResult;
    
                foreach (DataRow userRow in userTable.Rows)
                {
                    int allocAlready = 0;
                    foreach (DataRow allocRow in pTable.Rows)
                    {
                        if (allocRow["FeeEarner"].ToString() == userRow["UserID"].ToString())
                        {
                            allocAlready = 1;                            
                        }
                    }
    
                    if (allocAlready == 0)
                    {
                        string strUser = userRow["UserID"].ToString();          
                        decimal fees = cTimesheet.UserFees(strUser, pCaseNo);
                        int intCaseNo = Int32.Parse(pCaseNo);
                        if (fees > 0)
                        {
                            Object[] array = new object[8];
                            array[0] = 0;
                            array[1] = intCaseNo;
                            array[2] = DateTime.Today;
                            array[3] = strUser;
                            array[4] = fees;
                            array[5] = 0;
                            array[6] = fees;
                            array[7] = true;
                            newTable.Rows.Add(array);
                        }
                    }
                }
    
                foreach (DataRow row in pTable.Rows)
                {
                    newTable.ImportRow(row);
                }
    
                newTable.DefaultView.Sort = "AllocID";
                return newTable;
            }
    
            catch (Exception eX)
            {
                throw new Exception("cAllocation: Error in NewAllocations()" + Environment.NewLine + eX.Message);
            }
        }
    

    I think the key was using ImportRow rather than Rows.Add. I still use Rows.Add in my method but only when adding rows to a newly created table. I then loop through the existing table which was passed in as a paramater, and use ImportRow to add each row of the paramater table, to the newly created one. I then pass the new combined table out in my return statement, rather than a modified parameter table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want get from database table row name but in it i get error.
Why do I get error in this code?Even if i do not link but
I use the following jquery statements but i get error in this function onGetDataSuccess(result)
I am using following PHP code for trigger creation but always get error, please
I get error 1452 when trying to do this: ALTER TABLE test.potovanja ADD CONSTRAINT
I have get error with the following code and i can't figure it out.
I Get Error when i use the following code str = Insert into BasicData
In a php page I have following code: if($_REQUEST['c']!=) // I get error on
When connecting to my server (from a different machine) I get Error Code: 1044
I'm using HttpWebRequest and I get error when execute GetResponse(). I using this code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.