i got this problem when I want to update data in my sql from datagridview2 that already fill with doubleclickcell from datagridview1
this is the save button code
Try
'reader.Dispose()
Dim iReturn As Boolean
Dim conn As New MySqlConnection("server=localhost;user id=root;password=admin;database=dni")
For i As Integer = 0 To DataGridView2.Rows.Count - 1 Step +1
Using command As New MySqlCommand()
With command
.CommandText = "INSERT INTO dni.tbl_treatmen (`id_treatmen`, `nama_treatmen`, `harga_treatmen`, `Nama_obat`, `qty`, `satuan`) values (@id,@nama,@harga,@namaobat1,@qtyobat1,@satuanobat1) ON DUPLICATE KEY UPDATE
`id_treatmen` = @id,
`nama_treatmen`= @nama,
`harga_treatmen` = @harga,
`Nama_obat`= @namaobat1,
`qty`= @qtyobat1,
`satuan`= @satuanobat1"
.Connection = conn
.CommandType = CommandType.Text
.Parameters.AddWithValue("@id", textbox_id.Text)
.Parameters.AddWithValue("@nama", textbox_nama.Text)
.Parameters.AddWithValue("@harga", textbox_hargajual.Text)
.Parameters.AddWithValue("@namaobat1", DataGridView2.Rows(i).Cells(0).Value.ToString)
.Parameters.AddWithValue("@qtyobat1", DataGridView2.Rows(i).Cells(1).Value.ToString)
.Parameters.AddWithValue("@satuanobat1", DataGridView2.Rows(i).Cells(2).Value.ToString)
End With
Try
conn.Open()
command.ExecuteNonQuery()
iReturn = True
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
iReturn = False
Finally
conn.Dispose()
DataGridView2.DataSource = Nothing
End Try
End Using
Next
MessageBox.Show("Data Saved", "Save Data", MessageBoxButtons.OK, MessageBoxIcon.Information)
textbox_id.Text = "ID Treatmen"
textbox_nama.Text = "Nama Treatmen"
textbox_hargajual.Text = "Harga Jual"
textbox_id.Enabled = False
textbox_nama.Enabled = False
textbox_hargajual.Enabled = False
DataGridView1.Enabled = False
Label2.Visible = False
ShowData()
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = False
Button4.Enabled = True
Button5.Enabled = False
Button6.Enabled = False
If aa = 1 Then
DataGridView2.Rows.Clear()
DataGridView2.Columns.Clear()
GroupBox1.Visible = False
ElseIf aa = 2 Then
DataGridView2.DataSource = Nothing
Button9.Visible = False
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
this is the code when i double click cell
Button9.Visible = True
Dim conn = New MySqlConnection
conn.ConnectionString = "server=localhost;userid=root;password=admin;database=dni"
Dim SDA As New MySqlDataAdapter
Dim dbDataSet As New DataTable
Dim bSource As New BindingSource
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
textbox_id.Text = row.Cells("ID Treatmen").Value.ToString
textbox_nama.Text = row.Cells("Nama Treatmen").Value.ToString
textbox_hargajual.Text = row.Cells("Harga Treatmen").Value.ToString
Try
conn.Open()
Dim query As String
query = "select nama_obat as 'Nama Obat' , qty as 'Quantity' ,Satuan as 'Satuan' from dni.tbl_treatmen where id_treatmen = @idtreat"
Dim command As New MySqlCommand(query, conn)
SDA.SelectCommand = command
command.Parameters.AddWithValue("@idtreat", textbox_id.Text)
SDA.Fill(dbDataSet)
bSource.DataSource = dbDataSet
DataGridView2.DataSource = dbDataSet
SDA.Update(dbDataSet)
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
textbox_id.Enabled = False
textbox_nama.Enabled = True
textbox_hargajual.Enabled = True
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = True
Button4.Enabled = True
Button5.Enabled = True
Button6.Enabled = True
End If
this is my code, i want it whenever the datagridview2 is change and i press the save button it will update the record in mysql database. i use id_treatmen and nama_obat as primary key