我得到一个错误,从下面的代码“该行已经属于该表”:该行已经属于此表
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);
鉴于我创建了一个新的对象阵列中的每个代码进入循环的时候,我不明白为什么我收到此错误信息,这表明我同样的行被添加多次。为什么代码在每次由新对象数组生成行时都将每个循环看作是添加相同的数据行?
表中是否有主键? – 2012-08-15 06:28:02
它是一个断开的表 - 但没有 - 我没有指定任何列作为主键 - 是这个问题? – PJW 2012-08-15 06:53:28
也许吧。尝试每次输入不同的值并查看哪个字段导致了这个问题 – 2012-08-15 06:54:32