Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am making desktop application using C#.NET and SQL SERVER 2008.i have taken a listbox in a form containing elements which is fetched from database.Now i want that on selecting the item which is present in the listbox should get delete I have taken a delete button
i have listbox name listbox1 containing items and btndelete to delete
below is the coding that i have tried
private void btndeleteresources_Click(object sender, EventArgs e)
      {
          SqlDataAdapter adp = new SqlDataAdapter("delete  from  Resources_Required  where Resources='"+listBox1.SelectedIndex+"'", "Data Source=SW-PC-20;Initial Catalog=PSM;Integrated Security=True");
          DataSet ds = new DataSet();
          adp.Fill(ds, "Resources_Required");
          listBox1.DataSource = ds.Tables[0];
          listBox1.DisplayMember = "Resources";
          listBox1.ValueMember = "Resources";
          cboresources.Focus();
      }
Posted
Updated 3-May-11 21:07pm
v2
Comments
Sergey Alexandrovich Kryukov 4-May-11 3:02am    
Tag it! Forms, WPF, what?!
--SA

Hi,

Use this

Here "conn" is your connection
C#
private void btndeleteresources_Click(object sender, EventArgs e)
      {
          SqlCommand cmd = new SqlCommand("delete  from  Resources_Required  where Resources='"+listBox1.SelectedItems[0].ToString() +"'", conn);
          cmd.ExecuteNonQuery();
          SqlDataAdapter adp = new SqlDataAdapter("Select * from  Resources_Required","Data Source=SW-PC-20;Initial Catalog=PSM;Integrated Security=True" );
          DataSet ds = new DataSet();
          adp.Fill(ds, "Resources_Required");
          listBox1.DataSource = ds.Tables[0];
          listBox1.DisplayMember = "Resources";
          listBox1.ValueMember = "Resources";
          cboresources.Focus();
      }

Try it.

Regards
AR
 
Share this answer
 
v4
Comments
shivani 2013 4-May-11 3:21am    
no its not working....getting an error "listBox1.SelectedItems[0] does not contain definition of .Text"
Ankit Rajput 4-May-11 3:36am    
Sorry. Use
listBox1.SelectedItems[0].ToString();
shivani 2013 4-May-11 5:10am    
i have even tried before u wote..and this time i am getting an error
cannot find table[0]
Ankit Rajput 4-May-11 6:28am    
I have updated my answer try it.
shivani 2013 4-May-11 7:05am    
its not working I mean i am not getting any error while running but not deleting the elements too......and one more thing its listBox1.SelectedItems[0] not listBox1.SelectedItem[0] as it is giving error "cannt apply indexing with[] to an expression of type 'object' "
I'm not sure this code will delete or not.

Try this,
C#
string selCmdString = "SELECT *FROM Resources_Required";
SqlCommand selCommand = new SqlCommand(selCmdString, conn);

string delCmdString = "DELETE FROM Resources_Required WHERE Resources=@Res";
SqlCommand delCommand = new SqlCommand(delCmdString, conn);

SqlDataAdapter adp = new SqlDataAdapter(selCommand);
adp.DeleteCommand = delCommand;

//Adding one more thing, try this
DataRowView curRowView = (DataRowView)listBox1.SelectedItem;
String resources = curRowView["Resources"].ToString();  // Will return the current value in selected row of Column Resources

//Use SqlParameters to avoid SQL injections. Keep it as practice.
SqlParameter param = New SqlParameter();
param.ParameterName = "@Res";
//param.Value = listBox1.SelectedItem.ToString();
param.Value = resources;
param.SqlDbType = SqlDbType.SmallInt;
delCommand.Parameters.Add(param);

DataSet ds = new DataSet();
adp.Fill(ds,"Resources_Required");

DataTable dt = ds.Tables["Resources_Required"];
foreach (DataRow ro in dt.Rows) 
   {
    if (ro["Resources"].ToString() == resources) 
    {
        ro.Delete();
        break;
    }
   }
adp.Update(ds,"Resources_Required");
listBox1.DataSource = ds.Tables[0];
listBox1.DisplayMember = "Resources";
listBox1.ValueMember = "Resources";
cboresources.Focus();


Try now.
 
Share this answer
 
v6
Comments
shivani 2013 4-May-11 5:17am    
neah not working
no item was deleted
Tarun.K.S 4-May-11 6:36am    
I have updated the answer. Check now.
shivani 2013 4-May-11 7:16am    
getting error
instead of Tables() its Table[] in DataTable dt = ds.Tables("Resources_Required");
and warning
if (ro["Resources"] == listBox1.SelectedItem.ToString())
"Possible unintended reference comparison,to get a value comparison,cast the left hand side to type'string'
Tarun.K.S 4-May-11 7:23am    
Updated. You can check now.
shivani 2013 4-May-11 7:33am    
it doesnt contain any definition for .item in
if (ro.Item["Resources"] == listBox1.SelectedItem.ToString())
and one more thing
its SqlParameter param = new SqlParameter();
instead of
SqlParameter param = New SqlParameter;

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