Link to home
Start Free TrialLog in
Avatar of Connie Jerdet-Skehan
Connie Jerdet-SkehanFlag for United States of America

asked on

System.Data.ConstraintException error visual basic

I am a newbie
Using visual studio  and  visual basic form
I am trying to copy a selected row or rows from Datagridview and paste it to the same datagridview.
I can do this with no problem when the table DOES NOT have a primary key.
I now have a table with a primary key and get an System.Data.ConstraintException error "Column "id" is constrained to be unique. Value "1" is already present.
I assume I have to add 1 to the last record number but don't know how to do this or where to put in my code.

here is my code with help courtesy of Chinmay Patel

 Dim table = MyDataSet.MyTable

        For index = 0 To DataGridView1.SelectedRows.Count - 1

            Dim row As DataRow
            row = CType(DataGridView1.SelectedRows(index).DataBoundItem, DataRowView).Row

            Dim newRow As DataRow
            newRow = table.NewRow

            newRow.ItemArray = row.ItemArray

            table.Rows.Add(newRow)
        Next

I appreciate all help. I have spent days trying to figure out how to overcome this. Explanation and code fix would be highly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Connie Jerdet-Skehan

ASKER

the primary field  auto increments.