0

I am trying to update my database column, using data from datagridview column. I am using MySQL for database and vb.net for program.

This is my code:

cmd = New MySqlCommand("UPDATE tb_poinjalan 
      SET Bobot ='" & DataGridView2.Item(4, i).Value 
      & "' WHERE Kriteria='" & DataGridView2.Item(1, i).Value 
      & "';", Connector)

The problem is, this code only change first row in database column.

1
  • 1
    Perhaps only one row satisfies the criteria Commented Feb 27, 2019 at 6:22

1 Answer 1

1

I guess your data is only updating the rows where 'Kriteria' is met. And you need to make sure there is data in the DataGridView2 cell you are reading. However, the proper way to do it, to avoid any risk of injection is as below;

If Not(DataGridView2.Item(4, i).value is Nothing) Then

   sql = "UPDATE tb_poinjalan 
         SET Bobot = @Bobot WHERE Kriteria= @Criteria;"

  Try
    With conn
        .Connection = connector
        .CommandText = sql
        .Parameters.AddWithValue("@Bobot", DataGridView2.Item(4, i).Value)
        .Parameters.AddWithValue("@Criteria", DataGridView2.Item(1, i).Value)
    End With
    conn.ExecuteNonQuery()

   Catch ex as exception
     ..
   End Try
End If
Sign up to request clarification or add additional context in comments.

1 Comment

For more information about avoiding SQL injection in VB.NET, see here.

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.