Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (Module1.action == "Add")
                    {
                        SqlCommand com = new SqlCommand();
                        com.CommandText = "insert into Pack_Details values(@PdNo,@PNo,@Dscription,@Quantity,@Rate,@Deleted)";
                        com.Parameters.AddWithValue("@PdNo", cbpdno.Text);
                        com.Parameters.AddWithValue("@PNo", cbpno.Text);
                        com.Parameters.AddWithValue("@Dscription", txtdesc.Text);
                        com.Parameters.AddWithValue("@Quantity", txtqunt.Text);
                        com.Parameters.AddWithValue("@Rate", txtrate.Text);
                        //com.Parameters.AddWithValue("@UNo",Module1.st);
                        com.Parameters.AddWithValue("@Deleted", "0");
                        com.Connection = Module1.con;
                        try
                        {
                            Module1.con.Open();
                            com.ExecuteNonQuery();
                            MessageBox.Show("Record saved");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        Module1.con.Close();
                        fill();
                        binding();
                        Button(!Module1.b);
                        Locked(Module1.d);
                        // Clientid = cbpno.Text
                        return;
                    }
Posted
Updated 11-Apr-14 1:21am
v2
Comments
Vijay Tiwari 11-Apr-14 6:25am    
here pdno is an primary key
Maciej Los 11-Apr-14 6:51am    
Does pdno is AUTOINCREMENT field?

Quote:
Violation of PRIMARY KEY constraint 'PK_LogIn'. Cannot insert duplicate key in object 'dbo.LogIn'
It means you are breaking the rules of PrimaryKey and that is to insert another key, which is already present in a row.

Primary key can't be duplicate. When you explicitly insert that from the code, it should be unique that is it should different than all the key values present in that particular column.

If you insert something, which is already present, then this issue comes.
 
Share this answer
 
The error message says exactly what is wrong.
If pdno is autoincreament[^] and identity[^] field, remove it from insert list and check it again.
 
Share this answer
 
Comments
Vijay Tiwari 11-Apr-14 7:17am    
yes it is the autoincrement
Maciej Los 11-Apr-14 7:22am    
So, reove it from insert list ;)

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