单击按钮时出现C#错误

时间:2014-07-08 11:37:49

标签: c# visual-studio button error-handling

我正在使用VS C#开发一个带有GUI的程序,当我点击一个保存表格的按钮时我得到了错误

>   Gipsy3.exe!Gipsy3.Database1DataSet1TableAdapters.TableAdapterManager.UpdateAll(Gipsy3.Database1DataSet1 dataSet) Line 1262  C#
    Gipsy3.exe!Gipsy3.Form2.button3_Click(object sender, System.EventArgs e) Line 44 + 0x1a bytes   C#
    System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e) + 0x62 bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e) + 0x80 bytes   
    System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent) + 0xac bytes 
    System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) + 0x274 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x890 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m) + 0xeb bytes   
    System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m) + 0x20 bytes   
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x11 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x39 bytes  
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x5e bytes 
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) + 0x24d bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x155 bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x4a bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes  
    Gipsy3.exe!Gipsy3.Program.Main() Line 19 + 0x1d bytes   C#
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6b bytes    
    Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 bytes  
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x6f bytes   
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7 bytes  
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x16 bytes  
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x41 bytes    
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   
    [Native to Managed Transition]  

这是产生错误的代码

private void button3_Click(object sender, EventArgs e)
{
    this.Validate();
    this.itemsBindingSource.EndEdit();
    this.tableAdapterManager.UpdateAll(this.database1DataSet1); // error on this line
}

我无法从上面的堆栈调用中找到问题,有什么可以给我这个错误? 如果需要更多信息,请随时告诉我。

修改

catch (global::System.Exception ex) {
                workTransaction.Rollback();
                // ---- Restore the dataset -----------
                if (this.BackupDataSetBeforeUpdate) {
                    global::System.Diagnostics.Debug.Assert((backupDataSet != null));
                    dataSet.Clear();
                    dataSet.Merge(backupDataSet);
                }
                else {
                    if ((0 < allAddedRows.Count)) {
                        global::System.Data.DataRow[] rows = new System.Data.DataRow[allAddedRows.Count];
                        allAddedRows.CopyTo(rows);
                        for (int i = 0; (i < rows.Length); i = (i + 1)) {
                            global::System.Data.DataRow row = rows[i];
                            row.AcceptChanges();
                            row.SetAdded();
                        }
                    }
                }
                throw ex; // exception thrown here
            }
            finally {
                if (workConnOpened) {
                    workConnection.Close();
                }
                if ((this._itemsTableAdapter != null)) {
                    this._itemsTableAdapter.Connection = ((global::System.Data.SqlServerCe.SqlCeConnection)(revertConnections[this._itemsTableAdapter]));
                    this._itemsTableAdapter.Transaction = null;
                }
                if ((0 < adaptersWithAcceptChangesDuringUpdate.Count)) {
                    global::System.Data.Common.DataAdapter[] adapters = new System.Data.Common.DataAdapter[adaptersWithAcceptChangesDuringUpdate.Count];
                    adaptersWithAcceptChangesDuringUpdate.CopyTo(adapters);
                    for (int i = 0; (i < adapters.Length); i = (i + 1)) {
                        global::System.Data.Common.DataAdapter adapter = adapters[i];
                        adapter.AcceptChangesDuringUpdate = true;
                    }
                }
            }

以上是UpdateAll的代码,这是VS突出显示的部分,throw ex;以黄色突出显示。

编辑2

调试之后,我能够按照导致抛出异常的代码行。

// 
//---- Perform updates -----------
//
if ((this.UpdateOrder == UpdateOrderOption.UpdateInsertDelete)) {
  result = (result + this.UpdateUpdatedRows(dataSet, allChangedRows, allAddedRows));
  result = (result + this.UpdateInsertedRows(dataSet, allAddedRows));
} else {
  result = (result + this.UpdateInsertedRows(dataSet, allAddedRows));
  result = (result + this.UpdateUpdatedRows(dataSet, allChangedRows, allAddedRows)); // stepped in here
}
result = (result + this.UpdateDeletedRows(dataSet, allChangedRows));


/// <summary>
///Update rows in top-down order.
///</summary>
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
private int UpdateUpdatedRows(Database1DataSet1 dataSet, global::System.Collections.Generic.List<global::System.Data.DataRow> allChangedRows, global::System.Collections.Generic.List<global::System.Data.DataRow> allAddedRows) {
  int result = 0;
  if ((this._itemsTableAdapter != null)) {
    global::System.Data.DataRow[] updatedRows = dataSet.Items.Select(null, null, global::System.Data.DataViewRowState.ModifiedCurrent);
    updatedRows = this.GetRealUpdatedRows(updatedRows, allAddedRows);
    if (((updatedRows != null) && (0 < updatedRows.Length))) {
      result = (result + this._itemsTableAdapter.Update(updatedRows)); // stepped in here
      allChangedRows.AddRange(updatedRows);
    }
  }
  return result;
}


[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
[global::System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")]
public virtual int Update(global::System.Data.DataRow[] dataRows) {
  return this.Adapter.Update(dataRows); // stepped in here
}

在单步执行上面的代码之后,它会通过catch语句,然后跳过else,然后跳过if语句并抛出异常。

0 个答案:

没有答案