0

Problem: So this is not an uncommon question on coding forums but there seems to be a variety of ways in which people apply delete buttons and after days of attempts I'm asking for some advice regarding my method.

I have a simple app that takes some user input and uses it to to calculate holiday leave days. I have a simple SQLite db set up like so to handle new entries:

public class LeaveEntry
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public string Date { get; set; }
    public string Hours { get; set; }
    public string Days { get; set; }
}

I then set up the DB connection in this class and create some save and delete methods:

public class Database
{
    readonly SQLiteAsyncConnection _database;

    public Database (string dbPath)
    {
        _database = new SQLiteAsyncConnection(dbPath);
        _database.CreateTableAsync<LeaveEntry>().Wait();
    }

    public Task<List<LeaveEntry>> GetLeaveEntryAsync()
    {
        return _database.Table<LeaveEntry>().ToListAsync();
    }

    public Task<int> SaveLeaveEntry(LeaveEntry leaveEntry)
    {
        return _database.InsertAsync(leaveEntry);
    }

    public Task<int> DeleteLeaveEntry(LeaveEntry leaveEntry)
    {
        return _database.DeleteAsync(leaveEntry);
    }
}

Using the SaveLeaveEntry method I can successfuly add leave entries to a listview on another page but I'm having trouble deleting them. On this listview page I have added a menu item with a delete button when the user holds the listview item. When they select the delete menu item, I currently have it set to do this:

public void MenuItem_Clicked(object sender, EventArgs e)
    {
        LeaveEntry itemToDelete = ((sender as MenuItem).BindingContext as LeaveEntry);

        // Unfinished code attempt
        await App.Database.DeleteLeaveEntry(new LeaveEntry
        {
            ID = listView.SelectedItem.
        });
    }

In my mind, I am trying to use the selected item reference to pull that item from the db but it is simply doing nothing. I know I am missing something simple here but am getting a little burned out on the issue. Any help is appreciated. Can provide further code or info if required.

2
  • 1
    Your listView.Selected item is the whole LeaveEntry object? In that case you can just await App.Database.DeleteLeaveEntry((LeaveEntry)listView.SelectedItem); Commented Oct 11, 2021 at 14:09
  • 1
    why not just do App.Database.DeleteLeaveEntry(itemToDelete)? Commented Oct 12, 2021 at 0:38

2 Answers 2

1

In my mind, I am trying to use the selected item reference to pull that item from the db but it is simply doing nothing.

You should use the current holded item as the parameter of method DeleteLeaveEntry instead of creating a new item.

Please try the following code:

 LeaveEntry itemToDelete = ((sender as MenuItem).BindingContext as LeaveEntry);
 App.Database.DeleteLeaveEntry(itemToDelete);

Note:

In addition,if you don't trigger event ItemSelected of your listview,the value of listView.SelectedItem will be null.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, that's done it. I realise that the listview item is being deleted but not updating until I move away from the page and come back so I think I just need to add a refresh method or maybe trigger the OnAppearing method again. Thanks so much for all your help.
0

Basic Delete Method

using (SQLiteConnection conn = new SQLiteConnection(App.databaseLocation))
            {
                conn.CreateTable<DATA>();
                var list = conn.Table<DATA>().Where(x => x.text == "asd").ToList();
                for (int i = 0; i < list.Count; i++)
                {
                   conn.Delete(list[i]);
                }
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.