I have a small DataGridView which lets the user enter data.

And I’m trying this code to enter the data in the DataGridView to the database.
strQry = "INSERT INTO Emp_Fam_Details (empID, famName, famAge, famRelation, famOccup)" _
& "VALUES (@ID, @Name, @Age, @Rel, @Occ)"
Dim adapFam As New SqlDataAdapter
adapFam.InsertCommand = New SqlCommand(strQry, sqlcon)
For i As Integer = 0 To grdFamily.Rows.Count - 1
adapFam.InsertCommand.Parameters.AddWithValue("@ID", txtEmpID.Text.Trim)
adapFam.InsertCommand.Parameters.AddWithValue("@Name", grdFamily.Columns(0))
adapFam.InsertCommand.Parameters.AddWithValue("@Age", grdFamily.Columns(1))
adapFam.InsertCommand.Parameters.AddWithValue("@Rel", grdFamily.Columns(2))
adapFam.InsertCommand.Parameters.AddWithValue("@Occ", grdFamily.Columns(3))
result2 = adapFam.InsertCommand.ExecuteNonQuery()
Next
When I run this, I come across this error.
I found this solution here on SO but since I’m using Parameters not appended values, I’m having a hard time understanding how to do it with parameters.
Can anyone please show me how to do this?
Thank you all.
You cannot map columns to data. What you need to add is the row cell data of that column index
This is wrong
grdFamily.Columns(0)Parameter assignment within the for loop should be corrected as follows
NOTE Please change datatype as per your table.