简体   繁体   English

位置(-1)处无行-SQL Server尝试保存DataTable更改时

[英]No row at position (-1) - SQL Server when trying to save DataTable changes

I have a Windows Forms data entry applet for entering data into a small SQL Server database. 我有一个Windows窗体数据输入小程序,用于将数据输入到小型SQL Server数据库中。 I keep seeing this error when trying to save my new record after clicking AddNewItem button on the binding navigator component. 单击绑定导航器组件上的AddNewItem按钮后,尝试保存新记录时,我一直看到此错误。

错误信息

My code on clicking the save button on the binding navigator looks like this: 单击绑定导航器上的保存按钮时,我的代码如下所示:

private void btnSave_Click(object sender, EventArgs e)
{
        try
        {
            this.Validate();

            int currentPosition = this.witsStatusDBDataSet.TestCase.Rows.Count - 1;
            WitsStatusDBDataSet.TestCaseRow row = (WitsStatusDBDataSet.TestCaseRow)witsStatusDBDataSet.TestCase.Rows[currentPosition];
            row.AcceptChanges();

            witsStatusDBDataSet.TestCase.AcceptChanges();
            this.testCaseBindingSource.EndEdit();

            int current = witsStatusDBDataSet.TestCase.Rows.Count - 1;

            testCaseTableAdapter.Update(this.witsStatusDBDataSet.TestCase.Rows[current]);
            WitsStatusDBEntry.WitsStatusDBDataSetTableAdapters.TableAdapterManager manager = new TableAdapterManager();
            manager.UpdateAll(witsStatusDBDataSet);

            SysTimer = new System.Timers.Timer(2500);
            statusLabel1.Text = "Updated successfully.";
            SysTimer.Start();
        }
        catch(Exception exc)
        {
            string msg = exc.Message + " : " + exc.StackTrace;
            Clipboard.SetText(msg);
            MessageBox.Show(msg);
        }
}

If I enter the data manually in SQL Server Mgmt Studio, the binding navigator successfully loads it and I can use Move Next and Move Previous successfully. 如果我在SQL Server Mgmt Studio中手动输入数据,则绑定导航器将成功加载该数据,并且我可以成功使用Move Next和Move Previous。

But if I have a brand-new database that has just been deployed, with no records, I get this error. 但是,如果我刚部署了一个全新的数据库,没有任何记录,则会收到此错误。

I checked StackOverflow for similar issues, but nothing seemed to be the same situation. 我检查了StackOverflow是否存在类似问题,但似乎没有什么是相同的情况。

I re-coded the method, based on mason's comment. 我根据梅森的评论重新编码了该方法。 Here is the working code: 这是工作代码:

private void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            WitsStatusDBDataSet.TestCaseRow row = null;
            this.Validate();

            int currentPosition = this.witsStatusDBDataSet.TestCase.Rows.Count - 1;
            if(currentPosition == -1)
            {
                row = AddRowToDataTable(testCase: witsStatusDBDataSet.TestCase);
            }

            if (row == null)
            {
                currentPosition = this.witsStatusDBDataSet.TestCase.Rows.Count - 1;
            }
            row.AcceptChanges();

            witsStatusDBDataSet.TestCase.AcceptChanges();
            this.testCaseBindingSource.EndEdit();

            testCaseTableAdapter.Update(row);
            testCaseTableAdapter.InsertCase(row.Title, row.IsAutomated, row.Description, row.State, row.Area, row.Iteration, row.Priority,
                row.Severity, row.Owner, row.CreatedDate, row.ModifiedDate, row.TFS_Case_ID, row.TFSInstance);


            WitsStatusDBEntry.WitsStatusDBDataSetTableAdapters.TableAdapterManager manager = new TableAdapterManager();
            manager.Connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=WitsStatusDB;Integrated Security=True");
            manager.UpdateAll(witsStatusDBDataSet);

            SysTimer = new System.Timers.Timer(2500);
            statusLabel1.Text = "Updated successfully.";
            SysTimer.Start();
        }
        catch(Exception exc)
        {
            string msg = exc.Message + " : " + exc.StackTrace;
            Clipboard.SetText(msg);
            MessageBox.Show(msg);
        }
    }

Notice I had to call "AddRowToDataDable()" - that method simply transfers the contents of the Windows Form to the TestCaseRow object. 注意,我必须调用“ AddRowToDataDable()”-该方法只是将Windows窗体的内容传输到TestCaseRow对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM