[英]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.