c# - How can I delete a single entry from a table -


i have listbox usernames, , remove button want selected user (with entered data associated user) deleted when remove button clicked.

my code

sqlconnection con = new sqlconnection("data source=james-pc\\sqlexpress;initial catalog=staff;integrated security=true"); con.open();  string sql = @"delete staff1;"; sqlcommand cmd = new sqlcommand(sql, con); cmd.executenonquery(); con.close(); 

this code deletes whole table.

how can delete selected user?

you need clause select required record. have username of selected user deleted , pass @username parameter.

 var username = (listbox.selecteditem datarowview)["username"].tostring();  string sql = @"delete staff1 username = @username;";   sqlcommand cmd = new sqlcommand(sql, con);  cmd.parameters.addwithvalue("@username",usename);   cmd.executenonquery();  con.close(); 

see this thread on how use parameters in sql.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -