2012-08-15 34 views
2

我得到一个错误,从下面的代码“该行已经属于该表”:该行已经属于此表

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

鉴于我创建了一个新的对象阵列中的每个代码进入循环的时候,我不明白为什么我收到此错误信息,这表明我同样的行被添加多次。为什么代码在每次由新对象数组生成行时都将每个循环看作是添加相同的数据行?

+0

表中是否有主键? – 2012-08-15 06:28:02

+0

它是一个断开的表 - 但没有 - 我没有指定任何列作为主键 - 是这个问题? – PJW 2012-08-15 06:53:28

+0

也许吧。尝试每次输入不同的值并查看哪个字段导致了这个问题 – 2012-08-15 06:54:32

回答

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将参数表的每一行添加到新创建的表中。然后我在返回语句中传递新的组合表,而不是修改参数表。

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"); 
} 
相关问题