积极答复者
DataTable.Rows.Add(Item)是 概率性的 抛出ArgumentNullException异常

问题
-
public virtual int InsertItem(DataRow item, int pos) { DataValidator.CheckForNullReference(item, "item"); InstanceElement boundInstanceData = this.InstanceElement; DataTable boundNodeSet = this.DefaultDataTable; //如果item已经属于其他Table,则要重新生成一个Row,否则Add 或者InsertAt时会报错:该Row已经属于其他Table。 if (boundNodeSet != item.Table) { DataRow temp = boundNodeSet.NewRow(); temp.ItemArray = item.ItemArray; item = temp; } try { if (pos < 0) boundNodeSet.Rows.Add(item); else boundNodeSet.Rows.InsertAt(item, pos); foreach (DataRelation parentRelation in boundNodeSet.ParentRelations) { DataTable parentTable = parentRelation.ParentTable; //此处好像不需要手动处理,DateSet已经自动处理了,以后有时间了再验证去掉吧。 //TODO:修改数据Schema生成规则,去掉中间表后,修改此处处理 if (parentTable.TableName == boundNodeSet.TableName + GlobalConstant.ITEM_LIST_SUFFIX) { foreach (DataRelation realParentRelation in parentTable.ParentRelations) { DataTable realParentTable = realParentRelation.ParentTable; BindingSource bindingSource = boundInstanceData.DataDocument.GetBindingSource(realParentTable.TableName); DataRowView realParentRow = (bindingSource != null) ? bindingSource.Current as DataRowView : null; if (realParentRow != null) { DataRow parentRow = null; foreach (DataRowView viewRow in parentTable.DefaultView) { //针对实际情况,简单写吧,反正以后也要改 if (viewRow.Row[realParentRelation.ChildColumns[0].ColumnName].Equals(realParentRow[realParentRelation.ParentColumns[0].ColumnName])) { parentRow = viewRow.Row; break; } } if (parentRow != null) { for (int i = 0; i < parentRelation.ParentColumns.Length; i++) { item[parentRelation.ChildColumns[i].ColumnName] = parentRow[parentRelation.ParentColumns[i].ColumnName]; } } } } break; } } foreach (DataRelation childRelation in boundNodeSet.ChildRelations) { DataTable childTable = childRelation.ChildTable; //TODO:修改数据Schema生成规则,去掉中间表后,修改此处处理 if (childTable.TableName.IndexOf(GlobalConstant.ITEM_LIST_SUFFIX) > 0) { DataRow row = childTable.NewRow(); for (int i = 0; i < childRelation.ParentColumns.Length; i++) { row[childRelation.ChildColumns[i].ColumnName] = item[childRelation.ParentColumns[i].ColumnName]; } childTable.Rows.Add(row); } } //此处不能记录,因为有可能别人插入时就把数据准备好了,并直接调用保存 // RecordOriginalRow(item); // 在多值帮助多选插入多行时,导致InnerText死循环,根据查看对比及咨询老朱,把这行屏蔽掉。sunlixin 2011.9.1 //string xml = boundInstanceData.DataDocument.DocumentElement.InnerText;//TODO:有这句,你笑了,没有这句,你哭了。。。 return boundNodeSet.Rows.IndexOf(item); } catch { throw; } }
这段代码是我表格新增的时候调用的,在如下的位置
if (pos < 0) boundNodeSet.Rows.Add(item); 这句话概率性的抛出 ArgumentNullException的异常,而且Item都不为Null
boundNodeSet的datatable一行row都没有的时候才会出现这种情况,一旦有数据了这种情况就出现不了了。
异常信息如下:
{"值不能为 null。\r\n参数名: key"}
在 System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
在 System.Collections.Generic.Dictionary`2.get_Item(TKey key)
在 System.Data.DataView.MaintainDataView(ListChangedType changedType, DataRow row, Boolean trackAddRemove)
在 System.Data.DataViewListener.MaintainDataView(ListChangedType changedType, DataRow row, Boolean trackAddRemove)
在 System.Data.Index.<>c__DisplayClass5.<MaintainDataView>b__6(DataViewListener listener, ListChangedType type, DataRow row, Boolean track)
在 System.Data.Listeners`1.Notify[T1,T2,T3](T1 arg1, T2 arg2, T3 arg3, Action`4 action)
在 System.Data.Index.MaintainDataView(ListChangedType changedType, Int32 record, Boolean trackAddRemove)
在 System.Data.Index.InsertRecord(Int32 record, Boolean fireEvent)
在 System.Data.Index.ApplyChangeAction(Int32 record, Int32 action, Int32 changeRecord)
在 System.Data.Index.RecordStateChanged(Int32 record, DataViewRowState oldState, DataViewRowState newState)
在 System.Data.DataTable.RecordStateChanged(Int32 record1, DataViewRowState oldState1, DataViewRowState newState1, Int32 record2, DataViewRowState oldState2, DataViewRowState newState2)
在 System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Boolean suppressEnsurePropertyChanged, Int32 position, Boolean fireEvent, Exception& deferredException)
在 System.Data.DataTable.InsertRow(DataRow row, Int64 proposedID, Int32 pos, Boolean fireEvent)
在 System.Data.DataRowCollection.Add(DataRow row)
在 Genersoft.Platform.XFormController.ModelController.RepeatModelController.InsertItem(DataRow item, Int32 pos) 位置 E:\平台\XFormController\Dev\Apps\CDP\XFormController\src\XFormController_5.5\FormController\ModelController\RepeatModelController.cs:行号 668
答案
-
您好,
有找到以下這篇
有可能是有呼叫到 clear method。
或是那個 dataTable 的 dataView 所產生的問題。
- 已标记为答案 rxb1992 2015年9月16日 4:18
全部回复
-
你好,
从错误信息上看是参数null的情况,但你说又不是null。我推荐你在那段代码里面加上log日志。每次程序运行到的时候就往日志里面写上Item的信息,这样应该就能捕捉到item为null的情况。
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
您好,
有找到以下這篇
有可能是有呼叫到 clear method。
或是那個 dataTable 的 dataView 所產生的問題。
- 已标记为答案 rxb1992 2015年9月16日 4:18
-
您好,错误显示是参数为空的异常信息,请检查你每次插入的数据对应的参数不为空。希望我的解答对您有所帮助,谢谢!
- 已建议为答案 HippieZhou 2015年9月13日 2:27
-
您好,
有找到以下這篇
有可能是有呼叫到 clear method。
或是那個 dataTable 的 dataView 所產生的問題。
按这种方式看了看,没有clear,还是没有找到具体的原因 -
請問那個DataTable有什麼PK或是限制條件,或是有建立DataView嗎?
單一個DataTable新增應該是不會引發到 MaintainDataView Method。