0
private void button3_Click(object sender, EventArgs e)
    {
        if (textBox2.Text != "" & listBox1.SelectedIndex != -1)
        {
            string q = "update info set name='" + textBox2.Text.ToString() + "' where id " + listBox1.SelectedItem.ToString();
            dosomething(q);
            textBox2.Text = "";


        }
    }
private void dosomething(String q)
    {
        try {
            cn.Open();
            cmd.CommandText = q;
            cmd.ExecuteNonQuery();
            cn.Close();
            loaddata();

        }

Whenever I try to update a data from MS Access using my program I get this error: enter image description here

Is there something wrong with my code?

1
  • 1
    warning you are introducing sql injection attacks! Commented Sep 26, 2015 at 13:14

1 Answer 1

1

You need an operator between id " + listBox1.SelectedItem.ToString();. So:

id = " + listBox1.SelectedItem.ToString(); 

or whatever operator you want to use like >, >=, etc...

Also you have:

string q = "update info set name='" +

but it needs to be:

string q = "update info set name ='" +

notice I added a space between name and the = sign.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.