Hello,

I have a WPF/C# ListBox with one column with several items loaded from an SQL Sever Compact Edition database. I'm trying to delete a selected item from the database, but I'm having difficulties getting the value of the selected item, and because of this it is always returned null and the query although it doesn't fail, doesn't delete anything.

I've created the query using the VS query wizard and it looks like this:


DELETE FROM allBooks
WHERE ([Book Name] = 'book_NameListBox.SelectedItem.ToString()')


The database/connections/queries are not created programatically, they are all added from VS using the available wizards.

Following is the code behind the delete button:

private void Button_Click(object sender, RoutedEventArgs e)
        {
           bTA.DeleteQuery(); // bTA is the TableAdapter
           bTA.Fill(bDS.allBooks); //bDS is the Dataset
        }

I need some help on how to correctly call the value of the Selected Item in the ListBox so that the delete query would work.

thank you.

Try book_NameListBox.Text

the ListBox I'm using is a WPF control. I can not use the .Text method.

Try this:
DELETE FROM allBooks
WHERE ([Book Name] = 'book_NameListBox.SelectedItems[0].ToString()')

I've already tried that. The returned value is null.

This article has been dead for over six months. Start a new discussion instead.