2012-06-30 122 views
0

下面的代碼dr.SetAdded()語句賦予這樣的錯誤:錯誤setadded聲明

「setadded和SetModified之只能被稱爲與不變的DataRowState數據行」 現在又當如何?

 dBindSet.AcceptChanges() 


     For Each dt As DataTable In dBindSet.Tables 

    BindingContext(dt).EndCurrentEdit() 

      For Each dr As DataRow In dt.Rows 

        dr.SetAdded() 

        dr.Item("CREATEDON") = strServerDate 
        dr.Item("CREATEDBY") = iUserID 

       Next 
      Next 
+0

目前尚不清楚你想在這裏做什麼。首先,調用AcceptChanges,然後調用EndCurrentEdit(在哪一行?),然後嘗試將當前數據表中的每一行的行狀態設置爲Added(因爲每行都應該作爲數據庫中的新數據插入)可能如果你解釋什麼是你的意圖我們可以用不同的方法來回答。 – Steve

+0

我不想在數據表中添加任何新行,但我只想檢查數據表是否有任何行,然後更新存在行的「CREATEDON」,「CREATEDBY」的可數據列名稱。 – deep

+0

然後刪除'dr.SetAdded()'。在你的情況下是無用的。 – Steve

回答

0

方法SetAddedRowState更改爲已添加。在OP中,您正在更新字段,因此您不必調用此方法。

如果您想調用此方法,請檢查RowState

For Each dr As DataRow In dt.Rows 
    If dr.RowState=DataRowstate.Unchanged Then 
     dr.SetAdded() 
    End If 
    dr.Item("CREATEDON") = strServerDate 
    dr.Item("CREATEDBY") = iUserID 
Next 
+0

它顯示如下錯誤:未聲明rowstate。 – deep

0

如果我理解正確的話,你需要設置「CREATEDON」,並已在你的表被修改或插入的每一行的數據表的「CREATEDBY」領域。如果是這樣的話,你不應該叫AcceptChanges,因爲這會在每次的DataRow的RowState屬性重置爲Unchanged,你將無法處理的行改變(與數據庫更新會失敗)

' Don't call this, you will loose the RowState info.... 
'dBindSet.AcceptChanges() 
For Each dt As DataTable In dBindSet.Tables 
    ' Not really needed here. It serves to update the UI. 
    'BindingContext(dt).EndCurrentEdit() 
    For Each dr As DataRow In dt.Rows 
     if(dr.RowState = DataRowState.Added OrElse dr.RowState = DataRowState.Modified then    
      dr.Item("CREATEDON") = strServerDate 
      dr.Item("CREATEDBY") = iUserID 
     end if 
    Next 
Next