1
        private void btnDelete_Click(object sender, EventArgs e)
    {

      //dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

        OleDbConnection con = new OleDbConnection(constr);
        con.Open();

        OleDbCommand cmd = new OleDbCommand("DELETE FROM tb1 WHERE name='@Name'", con);
        cmd.Parameters.AddWithValue("@Name", txtproject_name.Text);


        cmd.ExecuteNonQuery();

        Bind();

        MessageBox.Show("deleted......");
        con.Close();
        }

This is my code for deleting the data from a database on a button click...but it is not deleting anything..can anyone help me with the code

7
  • Is your WHERE clause correct? I.e., are there any rows in the table that match txtproject_name.Text? Commented Jan 13, 2014 at 7:11
  • 2
    Another thought - try DELETE FROM tb1 WHERE name=@Name for your SQL. In other words, remove the single quotes - I believe AddWithValue will put them in for you. Commented Jan 13, 2014 at 7:12
  • yes ..there is a record Commented Jan 13, 2014 at 7:12
  • Put your exception ..... Commented Jan 13, 2014 at 7:12
  • is my syntax correct?? Commented Jan 13, 2014 at 7:12

3 Answers 3

6

Remove quotes around parameter:

"DELETE FROM tb1 WHERE name=@Name"
Sign up to request clarification or add additional context in comments.

4 Comments

@user3181292 yep, you should use just parameter name. Value of appropriate type (nvarchar, int, etc) will be passed to database and parameter will be replaced with constant value of appropriate type. E.g. if you are passing string "Bob", then @Name will have value 'Bob'
i have one more problem could you please help me out/?
@user3181292 if that problem is not related to current question, then it probably better to start another question (that might help other people who will have same problem in future)
@user3181292 if you are using parameter names without any quotes, then you have another problem, and I suggest you to ask new question for that, somebody will answer you :)
0

Try this

   private void btnDelete_Click(object sender, EventArgs e)
    {

      //dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

        OleDbConnection con = new OleDbConnection(constr);
        con.Open();

        OleDbCommand cmd = new OleDbCommand("DELETE FROM tb1 WHERE name=@Name", con);
        cmd.Parameters.AddWithValue("@Name", txtproject_name.Text);


        cmd.ExecuteNonQuery();

        Bind();

        MessageBox.Show("deleted......");
        con.Close();
        }

Comments

0

Try without the 2 ['] around @Name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.