Click here to Skip to main content
15,913,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey , i want to delete a selected "staff member" (displayed in listbox) / my code

C#
private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=JAMES-PC\\SQLEXPRESS;Initial Catalog=staff;Integrated Security=True");
            con.Open();

            string sql = @"DELETE FROM staff1 where Id=@name;";

            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@name", 1);

            cmd.ExecuteNonQuery();
            con.Close();
        }



but when i run the program i click the button (remove) but no luck, doesnt delete it
any help would be appreciated
also the table displayed in the listbox is (name)
Posted
Updated 5-Aug-15 22:53pm
v2
Comments
Suvendu Shekhar Giri 6-Aug-15 4:55am    
but you have hardcoded value for Id to be 1, is it for a purpose?
jamesmc1535 6-Aug-15 5:00am    
my friend helped me out with the code and he said i should add that part,so no i didnt insert that part i jus followed advice? / is it needed? how would i know if its needed?
Arasappan 6-Aug-15 5:11am    
put a break point on the open { and run the proram
jamesmc1535 6-Aug-15 5:21am    
did that,ran the program , got error ? must declare a scalar variable @name
Arasappan 6-Aug-15 5:27am    
can u please show ur table and parameters

Go to Web Config and paste this

XML
<connectionStrings>
    <add name="constring" connectionString="data source=JAMES-PC\\SQLEXPRESS; initial catalog=staff; persist security info= true; integrated security= true" providerName="System.Data.SqlClient"/>
  </connectionStrings>


and chane the code as follow

private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);
            con.Open();
 
            string sql = @"DELETE FROM staff1 where name=@name;";
 
            SqlCommand cmd = new SqlCommand("DELETE FROM staff1 where name=@name", con);
            cmd.Parameters.AddWithValue("@name", 1); or  //cmd.Parameters.AddWithValue("@name", listBox1.SelectedItem.ToString());
 
            cmd.ExecuteNonQuery();
            con.Close();
        }


Always go with id for deletion.Thank You:-)
 
Share this answer
 
v4
Comments
jamesmc1535 6-Aug-15 5:24am    
thanks ill do that quickly
C#
private void button2_Click(object sender, EventArgs e)
{
            SqlConnection con = new SqlConnection("Data Source=JAMES-PC\\SQLEXPRESS;Initial Catalog=staff;Integrated Security=True");
            con.Open();
 
            string sql = @"DELETE FROM staff1 where Id=@name;";
 
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@name", listBox1.SelectedValue.ToString());
 
            cmd.ExecuteNonQuery();
            con.Close();
}


see if the parameter id is varchar it is enclosed like this:
DELETE FROM staff1 where Id= 'staff1';
 
Share this answer
 
v4
Comments
jamesmc1535 6-Aug-15 5:26am    
Conversion failed when converting the nvarchar value 'james' to data type int. get a error
sreeyush sudhakaran 6-Aug-15 5:32am    
that means you need the id not staff name in the place , for that select both id , staff name from database and then set :
listBox1.DisplayMember = "staffname";//Name of the field as in Database
listBox1.ValueMember = "id";//Name of the field as in Database
listBox1.DataSource = Ds; // A Datasource filled by Querying the database
Arasappan 6-Aug-15 5:31am    
cmd.Parameters.AddWithValue("@name", int.Parse(listBox1.SelectedItem.ToString()));
or
cmd.Parameters.AddWithValue("@name", listBox1.SelectedValue.ToString());

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