Pregunta

i am working on a vb project . in this i need to save some record to one table and update some records in another table in one event or click .. i am doing like this .

dim simpan as new sqlcommand
conn = New SqlConnection(connectionstring)
conn.Open()
simpan = New SqlCommand()
simpan.Connection = conn
simpan.CommandType = CommandType.Text
simpan.CommandText = "update barang set (nama_barang,harga)values(" & TextBox3.Text & ",'" & TextBox4.Text & "') where kode_barang = '" & TextBox2.Text & "'"
simpan.ExecuteNonQuery()
tampil()
MsgBox("Data Berhasil Diubah", MsgBoxStyle.Information, "Informasi")
conn.Close()

but it giving error as "incorrect syntax near '('" .. i am not getting where i go wrong .. please help me

¿Fue útil?

Solución 2

You can't use update like this, change your code like so:

 simpan.CommandText = "update barang set nama_barang = '" & TextBox3.Text & "',harga ='" & TextBox4.Text & "' where kode_barang = '" & TextBox2.Text & "'"
 simpan.ExecuteNonQuery()

Otros consejos

I see a couple issues with this...

  • Your Syntax is wrong on your update statement (Al-3sli beat me to that one).
  • Your textbox values will cause issues if a user types a single quote in the text box (For Example: The word "Wasn't".
    • Add the replace function to each textbox TextBox3.text.Replace("'","''") That will replace single ticks with two single ticks.

You might also consider using parameterized queries

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top