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.

Recommended Answers

All 4 Replies

Try book_NameListBox.Text

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.