Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code

----------------------

C#
private void btnDelete_Click(object sender, EventArgs e)
       {
           SQLiteConnection connection = new SQLiteConnection(connectionString);

             delcmd.CommandText = "DELETE FROM Table WHERE ID='" + Listbox1.ToString() +"'";
               connection.Open();
               delcmd.Connection = connection;
               delcmd.ExecuteNonQuery();
               connection.Close();
               bindListbox1();
               Listbox1.Update();
               MessageBox.Show("Row Deleted");

                  }
Posted
Updated 17-Jul-12 2:52am
v2
Comments
Sebastian T Xavier 17-Jul-12 8:53am    
Whats the error you are getting....
rockpune 17-Jul-12 8:57am    
no error but no deleting any item
Sebastian T Xavier 17-Jul-12 9:11am    
Not getting deleted from database or from listbox?
rockpune 17-Jul-12 10:26am    
both delete from database and listbox
StianSandberg 17-Jul-12 9:19am    
The generated sql from your code will be:
"DELETE FROM Table WHERE ID = 'System.Windows.Forms.ListBox Items.Count:21,Items[0]:Div3'" You have to read the SelectedItem property of your listbox. Listbox1.SelectedValue

Listbox1.ToString() wont give you what you need..

try with Listbox1.SelectedValue

C#
private void btnDelete_Click(object sender, EventArgs e)
{
   SQLiteConnection connection = new SQLiteConnection(connectionString); 
   SQLiteCommand delcmd = new SQLiteCommand(); // did you miss this?
   delcmd.CommandText = "DELETE FROM Table WHERE ID='" + Listbox1.SelectedValue +"'";
   connection.Open();
   delcmd.Connection = connection;
   delcmd.ExecuteNonQuery();
   connection.Close();
   bindListbox1();
   Listbox1.Update();
   MessageBox.Show("Row Deleted");

}
 
Share this answer
 
v3
Comments
rockpune 17-Jul-12 9:03am    
ya i tryed still not getting
StianSandberg 17-Jul-12 9:04am    
is it a asp.net web application or a windows form?
rockpune 17-Jul-12 9:04am    
windowsform C#
StianSandberg 17-Jul-12 9:06am    
ok. Try to MessageBox.Show(Listbox1.ToString()) to see if you actually have the correct value from your dropdown.. I don't think Listbox1.ToString() will give you what you are looking for..
StianSandberg 17-Jul-12 9:12am    
Then you can try this:

private void btnDelete_Click(object sender, EventArgs e)
{
var selectedValue = Convert.ToString(Listbox1.SelectedValue);
MessageBox.Show("Selected value is" + selectedValue);
}
C#
delcmd.CommandText = "DELETE FROM Table WHERE ID='" + Listbox1.ToString() +"'";


  1. Use proper parameterised commands in SQL. Concatenating strings in this way can expose you to SQL injection attacks and the loss or corruption of your database.
  2. Why are you trying to convert the entire listbox[^] to a string and pass it into your command?


[edit]
Fixed bad link.
[/edit]
 
Share this answer
 
v2
Comments
rockpune 17-Jul-12 9:07am    
Server not found your link
Richard MacCutchan 17-Jul-12 10:43am    
Fixed. More importantly, do you see what is wrong with your code?
Richard MacCutchan 17-Jul-12 11:00am    
Why are you telling me this?

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