C#DataTable内部索引已损坏:在Windows窗体中保存时为“ 5”

时间:2019-01-09 21:03:15

标签: c# sql winforms

我有一个小表格,它连接到一个数据源(SQL Server数据库),因为它对两个表有两个绑定。

输入数据时:

Reg (A read only list box from table `vehicles`)
ID (A Read only auto generated id which increments from the max id in table 'serv')
Desc (Text input, table 'serv')
Date (Datetime picker, table 'serv')

如果我输入的数据具有将来的日期,则可以正常工作;如果我使用以前输入的具有将来日期的记录后使用过去的日期,则会出现错误:

  

System.InvalidOperationException:DataTable内部索引已损坏:“ 5”。

记录输入测试:

  • 未来,然后未来=确定
  • 过去,然后过去=确定
  • 过去,然后未来=确定
  • 未来然后过去=错误

我不知道为什么这么简单的表格会出现这种情况。

完全错误:

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidOperationException: DataTable internal index is corrupted: '5'.
   at MYPROGRAM.MYPROGRAMDataSetTableAdapters.TableAdapterManager.UpdateAll(MYPROGRAMDataSetdataSet) in C:\MYPROGRAM\MYPROGRAMDataSet.Designer.cs:line 11240
   at MYPROGRAM.NewService.servicesBindingNavigatorSaveItem_Click(Object sender, EventArgs e) in C:\MYPROGRAM\NewService.cs:line 50
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3260.0 built by: NET472REL1LAST_C
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
MYPROGRAM
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/MYPROGRAM.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3221.0 built by: NET472REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3314.0 built by: NET472REL1LAST_B
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3056.0 built by: NET472REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3056.0 built by: NET472REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3221.0 built by: NET472REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3056.0 built by: NET472REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Data
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3260.0 built by: NET472REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Data.DataSetExtensions
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3056.0 built by: NET472REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Data.DataSetExtensions/v4.0_4.0.0.0__b77a5c561934e089/System.Data.DataSetExtensions.dll
----------------------------------------
System.Numerics
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3056.0 built by: NET472REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
----------------------------------------
Accessibility
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3056.0 built by: NET472REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
System.Transactions
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3221.0 built by: NET472REL1LAST_C
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3056.0 built by: NET472REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.EnterpriseServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
System.Runtime.Caching
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3056.0
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Caching/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Runtime.Caching.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

保存按钮代码:

private void servicesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
    RegTextBox.Visible = true;
    RegTextBox.Text = regComboBox.Text;
    RegTextBox.Visible = false;
    RegTextBox.Visible = true;

    string RText = RegTextBox.Text;

    SqlConnection conn = new SqlConnection("SQLCON");

    DataSet ds = new DataSet();

    SqlDataAdapter da = new SqlDataAdapter(SQLQuery, conn);
    da.Fill(ds);

    RegTextBox.Visible = false;
    this.Validate();
    this.servicesBindingSource.EndEdit();
    this.tableAdapterManager.UpdateAll(this.MYPROGRAMDataSet);

    bindingNavigatorAddNewItem.Visible = true;
    servicesBindingNavigatorSaveItem.Visible = false;

    regComboBox.Visible = false;
    serviceIDTextBox.Visible = false;
    service_TypeTextBox.Visible = false;
    service_DescriptionTextBox.Visible = false;
    service_DateDateTimePicker.Visible = false;

    MessageBox.Show("Service Saved", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

    // this.servicesTableAdapter.Fill(this.MYPROGRAMDataSet.Services); (Commented out as not needed as I reopen form below)
    NewService.ActiveForm.Close();

    var newForm = new NewService();
    newForm.Show();
}

TableAdapterManager.UpdateAll(MYPROGRAMDataSetdataSet)(向下到第11240行)

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "15.0.0.0")]
    public virtual int UpdateAll(MYPROGRAMDataSet dataSet) {
        if ((dataSet == null)) {
            throw new global::System.ArgumentNullException("dataSet");
        }
        if ((dataSet.HasChanges() == false)) {
            return 0;
        }
        if (((this._consumablesTableAdapter != null) 
                    && (this.MatchTableAdapterConnection(this._consumablesTableAdapter.Connection) == false))) {
            throw new global::System.ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection s" +
                    "tring.");
        }
        if (((this._employeesTableAdapter != null) 
                    && (this.MatchTableAdapterConnection(this._employeesTableAdapter.Connection) == false))) {
            throw new global::System.ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection s" +
                    "tring.");
        }
        if (((this._holidaysTableAdapter != null) 
                    && (this.MatchTableAdapterConnection(this._holidaysTableAdapter.Connection) == false))) {
            throw new global::System.ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection s" +
                    "tring.");
        }
        if (((this._servicesTableAdapter != null) 
                    && (this.MatchTableAdapterConnection(this._servicesTableAdapter.Connection) == false))) {
            throw new global::System.ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection s" +
                    "tring.");
        }
        if (((this._vehiclesTableAdapter != null) 
                    && (this.MatchTableAdapterConnection(this._vehiclesTableAdapter.Connection) == false))) {
            throw new global::System.ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection s" +
                    "tring.");
        }
        if (((this._damagesTableAdapter != null) 
                    && (this.MatchTableAdapterConnection(this._damagesTableAdapter.Connection) == false))) {
            throw new global::System.ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection s" +
                    "tring.");
        }
        if (((this._versionTableAdapter != null) 
                    && (this.MatchTableAdapterConnection(this._versionTableAdapter.Connection) == false))) {
            throw new global::System.ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection s" +
                    "tring.");
        }
        if (((this._trailersTableAdapter != null) 
                    && (this.MatchTableAdapterConnection(this._trailersTableAdapter.Connection) == false))) {
            throw new global::System.ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection s" +
                    "tring.");
        }
        if (((this._trailerSITableAdapter != null) 
                    && (this.MatchTableAdapterConnection(this._trailerSITableAdapter.Connection) == false))) {
            throw new global::System.ArgumentException("All TableAdapters managed by a TableAdapterManager must use the same connection s" +
                    "tring.");
        }
        global::System.Data.IDbConnection workConnection = this.Connection;
        if ((workConnection == null)) {
            throw new global::System.ApplicationException("TableAdapterManager contains no connection information. Set each TableAdapterMana" +
                    "ger TableAdapter property to a valid TableAdapter instance.");
        }
        bool workConnOpened = false;
        if (((workConnection.State & global::System.Data.ConnectionState.Broken) 
                    == global::System.Data.ConnectionState.Broken)) {
            workConnection.Close();
        }
        if ((workConnection.State == global::System.Data.ConnectionState.Closed)) {
            workConnection.Open();
            workConnOpened = true;
        }
        global::System.Data.IDbTransaction workTransaction = workConnection.BeginTransaction();
        if ((workTransaction == null)) {
            throw new global::System.ApplicationException("The transaction cannot begin. The current data connection does not support transa" +
                    "ctions or the current state is not allowing the transaction to begin.");
        }
        global::System.Collections.Generic.List<global::System.Data.DataRow> allChangedRows = new global::System.Collections.Generic.List<global::System.Data.DataRow>();
        global::System.Collections.Generic.List<global::System.Data.DataRow> allAddedRows = new global::System.Collections.Generic.List<global::System.Data.DataRow>();
        global::System.Collections.Generic.List<global::System.Data.Common.DataAdapter> adaptersWithAcceptChangesDuringUpdate = new global::System.Collections.Generic.List<global::System.Data.Common.DataAdapter>();
        global::System.Collections.Generic.Dictionary<object, global::System.Data.IDbConnection> revertConnections = new global::System.Collections.Generic.Dictionary<object, global::System.Data.IDbConnection>();
        int result = 0;
        global::System.Data.DataSet backupDataSet = null;
        if (this.BackupDataSetBeforeUpdate) {
            backupDataSet = new global::System.Data.DataSet();
            backupDataSet.Merge(dataSet);
        }
        try {
            // ---- Prepare for update -----------
            //
            if ((this._consumablesTableAdapter != null)) {
                revertConnections.Add(this._consumablesTableAdapter, this._consumablesTableAdapter.Connection);
                this._consumablesTableAdapter.Connection = ((global::System.Data.SqlClient.SqlConnection)(workConnection));
                this._consumablesTableAdapter.Transaction = ((global::System.Data.SqlClient.SqlTransaction)(workTransaction));
                if (this._consumablesTableAdapter.Adapter.AcceptChangesDuringUpdate) {
                    this._consumablesTableAdapter.Adapter.AcceptChangesDuringUpdate = false;
                    adaptersWithAcceptChangesDuringUpdate.Add(this._consumablesTableAdapter.Adapter);
                }
            }
            if ((this._employeesTableAdapter != null)) {
                revertConnections.Add(this._employeesTableAdapter, this._employeesTableAdapter.Connection);
                this._employeesTableAdapter.Connection = ((global::System.Data.SqlClient.SqlConnection)(workConnection));
                this._employeesTableAdapter.Transaction = ((global::System.Data.SqlClient.SqlTransaction)(workTransaction));
                if (this._employeesTableAdapter.Adapter.AcceptChangesDuringUpdate) {
                    this._employeesTableAdapter.Adapter.AcceptChangesDuringUpdate = false;
                    adaptersWithAcceptChangesDuringUpdate.Add(this._employeesTableAdapter.Adapter);
                }
            }
            if ((this._holidaysTableAdapter != null)) {
                revertConnections.Add(this._holidaysTableAdapter, this._holidaysTableAdapter.Connection);
                this._holidaysTableAdapter.Connection = ((global::System.Data.SqlClient.SqlConnection)(workConnection));
                this._holidaysTableAdapter.Transaction = ((global::System.Data.SqlClient.SqlTransaction)(workTransaction));
                if (this._holidaysTableAdapter.Adapter.AcceptChangesDuringUpdate) {
                    this._holidaysTableAdapter.Adapter.AcceptChangesDuringUpdate = false;
                    adaptersWithAcceptChangesDuringUpdate.Add(this._holidaysTableAdapter.Adapter);
                }
            }
            if ((this._servicesTableAdapter != null)) {
                revertConnections.Add(this._servicesTableAdapter, this._servicesTableAdapter.Connection);
                this._servicesTableAdapter.Connection = ((global::System.Data.SqlClient.SqlConnection)(workConnection));
                this._servicesTableAdapter.Transaction = ((global::System.Data.SqlClient.SqlTransaction)(workTransaction));
                if (this._servicesTableAdapter.Adapter.AcceptChangesDuringUpdate) {
                    this._servicesTableAdapter.Adapter.AcceptChangesDuringUpdate = false;
                    adaptersWithAcceptChangesDuringUpdate.Add(this._servicesTableAdapter.Adapter);
                }
            }
            if ((this._vehiclesTableAdapter != null)) {
                revertConnections.Add(this._vehiclesTableAdapter, this._vehiclesTableAdapter.Connection);
                this._vehiclesTableAdapter.Connection = ((global::System.Data.SqlClient.SqlConnection)(workConnection));
                this._vehiclesTableAdapter.Transaction = ((global::System.Data.SqlClient.SqlTransaction)(workTransaction));
                if (this._vehiclesTableAdapter.Adapter.AcceptChangesDuringUpdate) {
                    this._vehiclesTableAdapter.Adapter.AcceptChangesDuringUpdate = false;
                    adaptersWithAcceptChangesDuringUpdate.Add(this._vehiclesTableAdapter.Adapter);
                }
            }
            if ((this._damagesTableAdapter != null)) {
                revertConnections.Add(this._damagesTableAdapter, this._damagesTableAdapter.Connection);
                this._damagesTableAdapter.Connection = ((global::System.Data.SqlClient.SqlConnection)(workConnection));
                this._damagesTableAdapter.Transaction = ((global::System.Data.SqlClient.SqlTransaction)(workTransaction));
                if (this._damagesTableAdapter.Adapter.AcceptChangesDuringUpdate) {
                    this._damagesTableAdapter.Adapter.AcceptChangesDuringUpdate = false;
                    adaptersWithAcceptChangesDuringUpdate.Add(this._damagesTableAdapter.Adapter);
                }
            }
            if ((this._versionTableAdapter != null)) {
                revertConnections.Add(this._versionTableAdapter, this._versionTableAdapter.Connection);
                this._versionTableAdapter.Connection = ((global::System.Data.SqlClient.SqlConnection)(workConnection));
                this._versionTableAdapter.Transaction = ((global::System.Data.SqlClient.SqlTransaction)(workTransaction));
                if (this._versionTableAdapter.Adapter.AcceptChangesDuringUpdate) {
                    this._versionTableAdapter.Adapter.AcceptChangesDuringUpdate = false;
                    adaptersWithAcceptChangesDuringUpdate.Add(this._versionTableAdapter.Adapter);
                }
            }
            if ((this._trailersTableAdapter != null)) {
                revertConnections.Add(this._trailersTableAdapter, this._trailersTableAdapter.Connection);
                this._trailersTableAdapter.Connection = ((global::System.Data.SqlClient.SqlConnection)(workConnection));
                this._trailersTableAdapter.Transaction = ((global::System.Data.SqlClient.SqlTransaction)(workTransaction));
                if (this._trailersTableAdapter.Adapter.AcceptChangesDuringUpdate) {
                    this._trailersTableAdapter.Adapter.AcceptChangesDuringUpdate = false;
                    adaptersWithAcceptChangesDuringUpdate.Add(this._trailersTableAdapter.Adapter);
                }
            }
            if ((this._trailerSITableAdapter != null)) {
                revertConnections.Add(this._trailerSITableAdapter, this._trailerSITableAdapter.Connection);
                this._trailerSITableAdapter.Connection = ((global::System.Data.SqlClient.SqlConnection)(workConnection));
                this._trailerSITableAdapter.Transaction = ((global::System.Data.SqlClient.SqlTransaction)(workTransaction));
                if (this._trailerSITableAdapter.Adapter.AcceptChangesDuringUpdate) {
                    this._trailerSITableAdapter.Adapter.AcceptChangesDuringUpdate = false;
                    adaptersWithAcceptChangesDuringUpdate.Add(this._trailerSITableAdapter.Adapter);
                }
            }
            // 
            //---- 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));
            }
            result = (result + this.UpdateDeletedRows(dataSet, allChangedRows));
            // 
            //---- Commit updates -----------
            //
            workTransaction.Commit();
            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();
                }
            }
            if ((0 < allChangedRows.Count)) {
                global::System.Data.DataRow[] rows = new System.Data.DataRow[allChangedRows.Count];
                allChangedRows.CopyTo(rows);
                for (int i = 0; (i < rows.Length); i = (i + 1)) {
                    global::System.Data.DataRow row = rows[i];
                    row.AcceptChanges();
                }
            }
        }
        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;

1 个答案:

答案 0 :(得分:2)

Ok you tried and you know

Switch between the default

TableAdapterManager.UpdateOrderOption.InsertUpdateDelete

to

TableAdapterManager.UpdateOrderOption.UpdateInsertDelete