简体   繁体   中英

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.

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.'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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