Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am inserting records into sql table using c# window based application.
both have primary and foreign key relation.

once I insert the records I am getting this error:
Violation of PRIMARY KEY constraint 'PK__Configur__1CDF71AC5AB5B85B'. Cannot insert duplicate key in object 'dbo.ConfigurationDetails'. The duplicate key value is (XCD213123).


my codes are blow. please refer some using links same as this codes

What I have tried:

C#
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DCID;Integrated Security=True");

string query = "insert into ConfigurationDetails(SystemserialNo,SystemPartsDetails,manufactured_Details) values('"
    + textBox1.Text
    + "','"
    + textBox2.Text
    + "','"
    + textBox3.Text
    + "')";
    
string query1 = "select * from SystemDetails";

SqlCommand cmd1 = new SqlCommand(query1,con);
SqlCommand cmd  = new SqlCommand(query, con);

SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
SqlDataAdapter da  = new SqlDataAdapter(cmd);

DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();

da.Fill(ds1, "ConfigurationDetails");
da1.Fill(ds2, "SystemDetails");

foreach (DataRow row in ds1.Tables["ConfigurationDetails"].Rows)
{
    if (row == ds2.Tables["SystemDetails"].Rows[0]["SystemserialNo"])
    {
        MessageBox.Show("yes");
    }
    else
    {
        MessageBox.Show("No");
    }
    
}
Posted
Updated 4-Feb-16 9:20am
v3
Comments
Sascha Lefèvre 4-Feb-16 14:52pm    
What is the primary key of your table ConfigurationDetails?
faizyab 2009 4-Feb-16 14:58pm    
SystemserialNo is the primary key
Richard Deeming 4-Feb-16 15:10pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
StM0n 4-Feb-16 15:22pm    
my guess, you're trying to insert a primary key a second time...

1 solution

As SystemserialNo is the primary key of your table ConfigurationDetails and you're letting the user enter the value for it (in textBox1) it's really trivial: You entered a primary key which already existed in that table.

If you entered it a second time because there actually is a second item/part that has the same SystemserialNo then your choice of SystemserialNo as the primary key isn't right.

If there can't be a second item/part with the same SystemserialNo then you need to check that first before attempting to insert what the user entered and if it already exists, notify the user without inserting it into the database.

Apart from that:
- Don't use string-concatenation for SQL-statements. Your code becomes susceptible to SQL-injection and less readable/maintainable. Use SQL-parameters[^] instead.
- Give your controls (textBox1 - 3 here) meaningful names (all designer generated identifiers in general). That's easy with the F2-refactoring of Visual Studio.
- Validate user input before relying on its validity (unless you've just not shown it here, the TextBox-inputs could be empty or otherwise invalid).
- Use using-statements with your Sql*****-objects in order to safely free the allocated system resources.
 
Share this answer
 
Comments
ZurdoDev 4-Feb-16 15:14pm    
+5
StM0n 4-Feb-16 15:24pm    
+5
StM0n 4-Feb-16 15:24pm    
+5

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