I've got quite the frustrating issue! My VB code below is intended to transfer the records in a DataGridView to an Access database. Let's say, for example, the gridview has three records
Name|House|Age John|L|1 Rick|B|12 Fred|A|20
The 'name|house|age' is the header of the GridView, whilst the three records make up the body. In my logic below, I get, in the Access database, John's record three times (i.e., instead of having John, Rick and Fred, I just have the exact same John, L, 1 three times).
I'm sure this is a small logic error that I've overlooked - so a fresh perspective and pair of eyes would be great. I've only included what I think to be the source of the problem, but happy to include more source if the problem potentially lies outside of this.
I appreciate your help!
Dim con As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =Jack.mdb") Dim cmd As New OleDb.OleDbCommand("INSERT INTO students(StuName,House,Age) VALUES (@StuName,@House,@Age)", con) Dim studentName As String Dim studentHouse As String Dim studentAge As String con.Open() For x As Integer = 1 To DataGridView1.Rows.Count - 1 studentName = DataGridView1.Rows(x).Cells(0).Value studentHouse = DataGridView1.Rows(x).Cells(1).Value studentAge = DataGridView1.Rows(x).Cells(2).Value Try cmd.Parameters.AddWithValue("@StuName", SqlDbType.Text).Value = studentName cmd.Parameters.AddWithValue("@House", SqlDbType.Text).Value = studentHouse cmd.Parameters.AddWithValue("@Age", SqlDbType.Text).Value = studentAge Catch ex As Exception End Try cmd.ExecuteNonQuery() Next x con.Close()