Home > Mobile >  Error fixing my MS Access Database in Visual C#
Error fixing my MS Access Database in Visual C#

Time:12-14

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();
    }
}

CodePudding user response:

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

  • Related