vendredi 18 septembre 2015

Cannot delete the selected item from listview (values in sqlite)

I am trying to delete a selected item from list view. The values are stored in database. I tried it in sqlite. How to delete the values present in database and update the listview

  private void Button_Click_3(object sender, RoutedEventArgs e)
    {
        var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";


        var con = new SQLiteAsyncConnection(dbpath);

       var stt=con.QueryAsync<list>("delete from list where list1='"+list_view.SelectedItem+"'");
       con.DeleteAsync(stt);

       update();

    }

    public async void update()
    {
        var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
        var con = new SQLiteAsyncConnection(dbpath);

        List<list> mylist = await con.QueryAsync<list>("select * from list");
        if (mylist.Count != 0)
        {
            list_view.ItemsSource = mylist;
            list_view.DisplayMemberPath = "list1";
        }

    }

Here list1 is the column name and list is the table name

I placed a breakpoint and executed the code. It showed an exception ( Cannot delete Task`1: it has no PK) I think PK is column name in the exception once check the below code from sqlite class where the exception triggered

public int Delete (object objectToDelete)
    {
        var map = GetMapping (objectToDelete.GetType ());
        var pk = map.PK;
        if (pk == null) {
            throw new NotSupportedException ("Cannot delete " + map.TableName + ": it has no PK");
        }
        var q = string.Format ("delete from \"{0}\" where \"{1}\" = ?", map.TableName, pk.Name);
        return Execute (q, pk.GetValue (objectToDelete));
    }

Aucun commentaire:

Enregistrer un commentaire