Reputation: 744
I have a problem with updating record. I want to update 2 record in 1 column in one button click with two parameters. this is the code :
private void Button_Click(object sender, EventArgs e)
{
int use = Convert.ToInt32(textBox1.Text);
perintahsql = new SqlCeCommand("UPDATE Barang SET Pakai = @Pakai WHERE Nama_Barang = 'Mie Rebus' AND Nama_Barang = 'Telor'", koneksi);
perintahsql.Parameters.Clear();
perintahsql.Parameters.AddWithValue("@Pakai", use);
}
How to overcome this problem? Thanks.
Upvotes: 2
Views: 1593
Reputation: 3703
You can do this by changing the AND to an OR operator in the WHERE clause.
private void Button_Click(object sender, EventArgs e)
{
int use = Convert.ToInt32(textBox1.Text);
perintahsql = new SqlCeCommand("UPDATE Barang SET Pakai = @Pakai WHERE Nama_Barang = 'Mie Rebus' OR Nama_Barang = 'Telor'", koneksi);
perintahsql.Parameters.Clear();
perintahsql.Parameters.AddWithValue("@Pakai", use);
}
However, if you're going to be doing this for many values then its probably best to use
WHERE Nama_Barang IN ('name1','name2', ... ,'name99')
Upvotes: 0
Reputation: 14919
Your WHERE clause is looking for rows where Nama_Barang = 'Mie Rebus' AND Nama_Barang = 'Telor'
The key is the AND here. You don't have any rows where Nama_Barang is both 'Mie Rebus' and 'Telor'. You're looking for rows where Nama_Barang is either 'Mie Rebus' or 'Telor', so your WHERE clause should be Nama_Barang = 'Mie Rebus' OR Nama_Barang = 'Telor'
Upvotes: 1
Reputation: 40586
Change your SQL statement to:
UPDATE Barang SET Pakai = @Pakai WHERE Nama_Barang in ('Mie Rebus', 'Telor')
Upvotes: 2