我从以下代码中收到错误“此行已属于此表”:
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);
鉴于每次代码进入循环时我都会创建一个新的对象数组,我无法理解为什么我会收到此错误消息,这表明我正在多次添加同一行。当行每次由新的对象数组生成时,为什么代码将每个循环视为添加相同的数据行?