I have this datagridview whose rows are manually added on a click of a button. What I wanted to do was to somehow loop through each row and insert it in a mysql database
here is my current code:
Public Sub addResBulk()
Dim cmdAddRes As New MySqlCommand
Dim addResQry As String
'field remarks left empty until process complete
With cmdAddRes
.Parameters.AddWithValue("@ctrlno", ctrlNo)
.Parameters.AddWithValue("@resdate", dtp_resDate.Value)
.Parameters.AddWithValue("@timestart", cbo_start.SelectedValue)
.Parameters.AddWithValue("@timeend", cbo_end.SelectedValue)
.Parameters.AddWithValue("@claimdate", claimdate)
.Parameters.AddWithValue("@borrowerid", tb_borrowerID.Text)
.Parameters.AddWithValue("@resloc", tb_location.Text)
End With
For row As Integer = 0 To dgv_bulk.Rows.Count - 1
Try
addResQry = "INSERT INTO tbl_test(ControlNo, bCode, Qty, resDate, timeSTART, timeEND, claimDate, borrowerID, resLocation) VALUES " _
+ "(@ctrlno, @bcode, @qty, @resdate, @timestart, @timeend, @claimdate, @borrowerID, @resloc)"
If conn.State = ConnectionState.Open Then
conn.Close()
conn.Open()
Else
conn.Open()
End If
'dgv_bulk.Item(1, o).Value
With cmdAddRes
.Parameters.AddWithValue("@bcode", dgv_bulk.Item(1, row).Value)
.Parameters.AddWithValue("@qty", dgv_bulk.Item(2, row).Value)
qryRes = .ExecuteNonQuery
End With
conn.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
Next row
End Sub
However, an error gets in the way of successfully inserting each row into the database. it tells me that parameter @ctrlno is already defined. and another error telling me that i need a valid and open connection...
Any ideas?
Thanks in advance!