2

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.

1
  • And what is your problem? Commented Jan 11, 2013 at 9:32

3 Answers 3

2

Change your SQL statement to:

UPDATE Barang SET Pakai = @Pakai WHERE Nama_Barang in ('Mie Rebus', 'Telor')
Sign up to request clarification or add additional context in comments.

Comments

1

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'

Comments

0

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

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.