Click here to Skip to main content
15,793,624 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'd like to drop a table from c# listView. I don't want to specify the table name to Drop, but I want, when I select a table from listView, then that table can be deleted. I have 16 tables in sql database.

What I have tried:

SqlCommand cmd = new SqlCommand ("DROP TABLE" + listView.SelectedItem.Tostring()", conn)
Updated 18-Sep-22 20:56pm
PIEBALDconsult 18-Sep-22 16:07pm    
I would add some protection, such as ensuring that the provided value is the name of a table which is a candidate for deletion, not just any old table.


Looks like you need to include an additional space in the string.

Try "DROP TABLE " otherwise the resultant command would look like "drop tableyourtablename" instead of "drop table yourtablename"

Also look at what listView.SelectedItem returns.
Share this answer
Joel Silva 2022 19-Sep-22 2:52am    
Ok, thank you guys
You have an extra quote near the end of the line you posted. You only need quotes around the "DROP TABLE" part of the string.

Other than that, you provided no error messages you're getting or any description of a problem, so...

As for dropping a table, you already have the correct SQL statement, so unless you provide more details, it's impossible for anyone to tell you what you're doing wrong.
Share this answer
Joel Silva 2022 19-Sep-22 2:51am    
Thanks Dave...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900