Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Abort due to constraint violation PRIMARY KEY must be unique, any one tell me what error this.....
C#
private void tsbtnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                frmAddEditEvents obj = new frmAddEditEvents();
                obj.ShowDialog();
                    SQLiteConnection connection = new SQLiteConnection(connectionString);
                    if (grdEvents.Rows.Count > 1 && grdEvents.SelectedRows[0].Index != grdEvents.Rows.Count - 0)
                    {
                        delcmd.CommandText = "UPDATE AddEvent SET ID=" + grdEvents.SelectedRows[0].Cells[0].Value.ToString()+ "";
                        connection.Open();
                        delcmd.Connection = connection;
                        delcmd.ExecuteNonQuery();
                        connection.Close();  

                        grdEvents.Update();
                        MessageBox.Show("Row updated");
                    }
                else
                {
                    MessageBox.Show("Please select any record to update");
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Posted
Updated 13-Jul-12 3:40am
v2

Dear you are trying to update the ID field which you had defined Primary Key in you table structure. Primary key is always a unique and not supposed to insert a duplicate key and you are forcefully trying to update through a data which is already defined as a primary key.

Following is wrong.

SQL
delcmd.CommandText = "UPDATE AddEvent SET ID=" + grdEvents.SelectedRows[0].Cells[0].Value.ToString()+ "";


Thanks
Ashish
 
Share this answer
 
 
Share this answer
 
Hi darshan_ur,

The problem you stated above explains that you're trying to insert a duplicate value in the column which has a constraint of being Primary Key.

Check the value which you're passing to insert as ID, it must not be a duplicate n that's all.

Hope this helps.

Happy Coding :)
 
Share this answer
 
Comments
rockpune 13-Jul-12 6:34am    
i have 2 forms in form1 i have datagridview and second form some textboxs controls have but my proble is when i click on gridview row i want to display that row values into form2 for update bu here it showing


Abort due to constraint violation PRIMARY KEY must be unique
Sunny_Kumar_ 13-Jul-12 6:43am    
you get the error while fetching details or updating the table ?
rockpune 13-Jul-12 6:47am    
updating hte table
rockpune 13-Jul-12 6:47am    
updating the table
rockpune 13-Jul-12 6:49am    
i have only one button to save in form2 either i insert data or update data
If it is the problem with the Id column you can use Auto generated column I guess.
And I don think that you can update a primary key column with the same Data which is already in the database column
 
Share this answer
 

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