1

update data in access database using name two column

because one column have same data because SerialNumber and Start can be Repeat

that's make update in all row have same data

i use this code but i have syntax Error

private void button3_Click(object sender, EventArgs e)
    {

        try
        {


            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "update Timer set Stop='" + label1.Text + "'where (SerialNumber,Start)='" + comboBox1.Text + "','" + textBox1.Text + "' ";

            command.CommandText = query;
            command.ExecuteNonQuery();
            MessageBox.Show("Data saved");

            connection.Close();
            send_data f2 = new send_data(comboBox1.Text,label2.Text);
            f2.ShowDialog();
        }
        catch (Exception ex)
        {
            MessageBox.Show("ERORR" + ex);

        }
    }
2
  • 1
    Always use parameters to avoid SQL Injection. Commented Aug 22, 2014 at 21:40
  • In SQL a WHERE clause would compare the column values separately, e.g. where SerialNumber = @Foo and Start = @Bar. Probably not applicable to the software you have chosen. Commented Aug 22, 2014 at 21:49

1 Answer 1

1

The correct syntax for the WHERE clause is

 WHERE fieldname operator value AND/OR fieldname operator value ....

So the correct way to update that record is

string query = @"update Timer set Stop=? where SerialNumber = ? AND Start = ?";
command.CommandText = query;
command.Parameters.AddWithValue("@p1", label1.Text);
command.Parameters.AddWithValue("@p2", comboBox1.Text );
command.Parameters.AddWithValue("@p3", textBox1.Text);
command.ExecuteNonQuery();

Notice that before the WHERE keyword you need a space and I have changed your code to use a more secure parameterized approach instead of string concatenation

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

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.