这一行已经属于这张桌子

时间:2012-08-15 06:25:00

标签: c# ado.net

我从以下代码中得到错误'此行已属于此表':

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);
        }

当我单步执行代码时,我可以看到第二次出现错误时会访问以下行:

pTable.Rows.Add(array);

鉴于每次代码进入循环时我都会创建一个新的对象数组,我无法看到为什么我收到此错误消息,这表明我正在多次添加同一行。为什么代码将每个循环视为在每次由新对象数组生成行时添加相同的数据行?

2 个答案:

答案 0 :(得分:3)

另一种方法是在循环开始时创建一个NewRow(),分配它的数据,然后在循环的底部创建Rows.Add()。

{       
        // initialization code 
        // ...

        foreach (DataRow row in dt.Rows)
        {
            row.Delete();
        }
        Oda.Update(ds, "USERTABLE");

        DataRow  dr;

        foreach (var userRecord in urList)
        {
            dr = dt.NewRow();
            dr["username"] = userRecord.userName;
            dr["firstname"] = userRecord.firstName;
            dr["lastname"] = userRecord.lastName;
            dr["createdon"] = userRecord.createdOn;

            dt.Rows.Add(dr);
        }
        Oda.Update(ds, "USERTABLE");
}

答案 1 :(得分:1)

最终有效的代码是:

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);
        }
    }

我认为关键是使用ImportRow而不是Rows.Add。我仍然在我的方法中使用Rows.Add,但仅在向新创建的表中添加行时使用。然后我循环遍历作为参数传入的现有表,并使用ImportRow将参数表的每一行添加到新创建的表中。然后我在返回语句中传递新的组合表,而不是修改后的参数表。