Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written insert and update queries in DGV_rowleave event as
VB
sql = "insert into table1(Sno,BriefDescription,Jan) values(" & DGV(0, e.RowIndex).Value & ",'" & DGV(1, e.RowIndex).Value & "'," & DGV(2, e.RowIndex).Value & " )"
            adp.InsertCommand = sqlcon.CreateCommand()
            adp.InsertCommand.CommandText = sql
            adp.InsertCommand.ExecuteNonQuery()
            sql1 = "UPDATE table1 SET Year=" & cbyears.SelectedItem & " where Sno=" & DGV(0, e.RowIndex).Value.ToString() & " "
            adp.UpdateCommand = sqlcon.CreateCommand
            adp.UpdateCommand.CommandText = sql1
            adp.UpdateCommand.ExecuteNonQuery()

queries are executing fine and databse is also updating……

As I hve written this lines in dgv_rowleave event, every time this code is executing and getting error as:Validation of primary key constraint:cannot insert duplicate key..
Even while form(having DGV) loading also this error is coming..

When I perform changes to my DGV after loading the form then only row_leave event wants to be execute,how?

Please give any idea..
Posted
Updated 24-May-11 23:07pm
v2

1 solution

Validation of primary key constraint:cannot insert duplicate key..
The issue is with your table design and the query being used. Error says that your table has a primary key defined in it. Now, since a column has been defined a primary key, it will have only unique values. Based on the error, it looks like, you are trying to insert/update a row with already existing primary key column value.

Resolutions:
1. Either remove the field from updating/inserting from your query and let it auto-increment
2. Unmark the field as Primary key, if you really want to update/insert it and it can hold same values for multiple rows.
 
Share this answer
 
Comments
Espen Harlinn 25-May-11 16:39pm    
Good points, my 5
Sandeep Mewara 26-May-11 6:27am    
Thanks. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900