Violation of PRIMARY KEY constraint 'PK_tbl_Report2'.

Posted by Sudheep.grandhe under C# on 12/19/2011 | Points: 10 | Views : 1917 | Status : [Member] | Replies : 2
Hi,

it shows the following error.in my code where i have to change.

Violation of PRIMARY KEY constraint 'PK_tbl_Report2'. Cannot insert duplicate key in object 'dbo.tbl_report2'.
The statement has been terminated.

public void insertreport2data()
{
//string strondate, strondate1, ondate;
//string strinvoicedate, strinvoicedate1, invoicedate;
//char sepChar = '/';
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
//DBConnection db = new DBConnection();

//SqlConnection con = db.opencon();
SqlCommand cmd = new SqlCommand("insert into tbl_report2 (compname,compaddress,invoiceno,orderno,ondate,invoicedate,amountstatus,netamount,netvat,tamount)values(@compname,@compaddress,@invoiceno,@orderno,@ondate,@invoicedate,@amountstatus,@netamount,@netvat,@tamount)", con);
SqlCommand cmd2 = new SqlCommand("insert into tbl_report1 (particulars,quantity,perprice,amount,orderno,compname,invoicedate,ondate,invoiceid) select particulars,quantity,perprice,amount,orderno,compname,invoicedate,ondate,invoiceid from tbl_temp", con);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd.Parameters.AddWithValue("@invoiceno", txt_invoiceno.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
if (txt_invoicedate.Text != null && txt_invoicedate.Text != "")
{
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DBNull.Value);
}
if (txt_ondate.Text != null && txt_ondate.Text != "")
{
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}
cmd.Parameters.AddWithValue("@amountstatus", rb_paid.Checked);
cmd.Parameters.AddWithValue("@amount", rb_unpaid.Checked);
cmd.Parameters.AddWithValue("@netamount", txt_netamount.Text);
cmd.Parameters.AddWithValue("@netvat", txt_vat.Text);
cmd.Parameters.AddWithValue("@tamount", txt_totalamount.Text);
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
con.Close();

}

Best,
Sudheep.



Responses

Posted by: Sksingh on: 12/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,
That means you are trying to insert duplicate value for PK column (invoiceno) in tbl_report2 table.
Solution:
Check for your primary key that is invoiceno if present in table show a message to user that enter invoice no already exists in table.


Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 1/2/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
Primary key is not allowed existing value.So Please check your table. I think, First validate a Invoice no before click a save button .

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response