简体   繁体   English

在 Visual C# 中修复我的 MS Access 数据库时出错

[英]Error fixing my MS Access Database in Visual C#

I've been creating a project for an assignment using windows forms, essentially my program includes 3 forms: Login, Database viewer, and a separate form to edit the database.我一直在使用 windows forms 创建一个分配项目,基本上我的程序包括 3 个 forms:登录、数据库查看器和一个单独的表单来编辑数据库。

When editing the database: If the database is empty, and I try to delete a record twice my program breaks and I get the error: System.InvalidOperationException: 'Current item cannot be removed from the list because there is no current item.'编辑数据库时:如果数据库为空,并且我尝试删除一条记录两次我的程序中断,我收到错误: System.InvalidOperationException: 'Current item cannot be removed from the list because there is no current item.'

Is there any type of code I can implement to prevent the delete button from being used when the table is empty?是否有任何类型的代码可以实现以防止在表为空时使用删除按钮?

This is my code for the delete button of the database, there isn't really any other code to do with the database that I've changed myself as I wouldn't know what to do.这是我的数据库删除按钮的代码,实际上没有任何其他代码与我自己更改的数据库有关,因为我不知道该怎么做。

private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Are you sure you'd like to delete this record?", "Delete Record", MessageBoxButtons.YesNo) == DialogResult.Yes) 
    {
        this.computer_GamesBindingSource.RemoveCurrent();
        this.Validate();
        this.computer_GamesBindingSource.EndEdit();
    }
}

Try doing a check like this:尝试做这样的检查:

if (this.computer_GamesBindingSource.Count > 0)
{
    /* put delete code here */
}

This way the delete code is only executed if there are records.这样删除代码只有在有记录时才会执行。

For more info, see https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.bindingsource.count有关详细信息,请参阅https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.bindingsource.count

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

相关问题 连接到 MS Access 数据库 2000 - 2003 格式的 Visual Studio C# windows 窗体中的登录功能。 研究并尝试修复无济于事 - login function in a visual studio C# windows form connected to a MS Access database 2000 - 2003 format. Researched and tried fixing to no avail 在C#中显示MS Access数据库时出错 - Error in displaying MS Access Database in C# 无法从Visual C#将数据插入MS Access数据库 - Unable to insert data into MS Access database from Visual C# 语法错误插入语句可视化 C# 和 ms 访问 - Syntax Error Insert into statement visual C# and ms access C#转ms访问数据库 - C# to ms access database 从Visual Studio C#中的MS Access数据库中获取OLE(位图)对象形式,我的代码有什么问题? - Taking OLE (bitmap) object form MS Access database in Visual studio C# , What's wrong in my code? C#与MS Access数据库错误=使用未分配的局部变量 - C# with MS Access Database Error = Use of unassigned local variable 从C#Winform连接到MS Access数据库时出错 - Error connecting to MS Access database from C# Winform MS Access / accdb“无法打开数据库”错误(C#) - MS Access/accdb “Cannot open database” error (C#) Visual Studio 2015 中无法识别的数据库格式,在 C# 中使用 MS Access 数据库 - Unrecognised database format in Visual Studio 2015 with MS Access Database in for C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM