Private Sub EditButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditButton.Click
Dim r As Integer = 0
Dim de_id_check As String
Dim de_name As String
Dim de_result As String
Dim sqlselect As String
Dim cmdselect As New MySqlCommand
Dim selectnumrows As Integer
Dim sqlinsert As String
Dim cmdinsert As New MySqlCommand
Dim sqlupdate As String
Dim cmdupdate As New MySqlCommand
While (drugdetailDGV.RowCount - 1)
With drugdetailDGV
de_id_check = .Rows(r).Cells(0).Value.ToString
de_name = .Rows(r).Cells(1).Value
de_result = .Rows(r).Cells(2).Value
sqlselect = "select * from drug_detail where d_id='" & de_id_check & "' "
cmdselect.Connection = MyConnect
cmdselect.CommandText = sql
selectnumrows = cmdselect.ExecuteScalar()
If selectnumrows > 0 Then 'ถ้ามีอยู่แล้วดัพเดท
'MsgBox(selectnumrows)
sqlupdate = "update drug_detail set de_name='" & de_name & "',de_result='" & de_result & "'" + _
"where de_id='" & de_id_check & "'"
'MsgBox(sql)
cmdupdate = New MySqlCommand(sqlupdate, MyConnect)
cmdupdate.ExecuteNonQuery()
Else 'ถ้าไม่มีเพิ่มใหม่
sqlinsert = "insert into drug_detail(d_id,de_name,de_result," + _
"values('" & didupdate & "','" & de_name & "'," + _
"'" & de_result & "'" + _
")"
'MsgBox(sql)
cmdinsert = New MySqlCommand(sql, MyConnect)
cmdinsert.ExecuteNonQuery()
End If
End With
r += 1
End While
End Sub