Amr Ghonem
Amr Ghonem

Reputation: 43

update data in access database using name two column

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);

        }
    }

Upvotes: 1

Views: 738

Answers (1)

Steve
Steve

Reputation: 216303

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

Upvotes: 1

Related Questions