Student
Student

Reputation: 25

how to update column in database MySQL

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.

Upvotes: 0

Views: 98

Answers (1)

Nadeem_MK
Nadeem_MK

Reputation: 7699

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

Upvotes: 1

Related Questions