Can you tell me why im getting this insert error and how i would resolve it please
Violation of PRIMARY KEY constraint 'PK_CustomFieldsData'. Cannot insert duplicate key in object 'dbo.CustomFieldsData'.
The statement has been terminated.
EbillzNotesDataContext
var
where (p.CustomerCode == cuscode) && (p.FieldID == Convert.ToInt32(27))
select p).FirstOrDefault();
t.FieldValue = "PAYMENT PLAN"
t.LinkID = " Customer"
EB.SubmitChanges();
I'm only trying to update the table, not insert a new record????
Thanks for your help
Anthony
t = (from p in EB.CustomFieldsDatasEB = new EbillzNotesDataContext();
VulpesPosted Jul 8, 2011, 10:45 AM
VulpesPosted Jul 8, 2011, 11:17 AM
Anyway, pleased you've solved it, whatever the explanation was :)
Anthony ClarkePosted Jul 8, 2011, 11:06 AM
Your not going to believe this but out of curiosity i created a new class
pasted the code into in then tried again and it worked first time????
Stupid thing ha. Ill mark your answer as correct for your time and persistence though.
Thanks again Vulpes
VulpesPosted Jul 8, 2011, 10:35 AM
t.FieldValue = "PAYMENT PLAN" ;
because 't' would be null.
Just as an experiment, try replacing FirstOrDefault() in the query with Single().
Anthony ClarkePosted Jul 8, 2011, 10:04 AM
Yeah, if i comment it out i still seem to get the error message????
VulpesPosted Jul 8, 2011, 9:56 AM
t.LinkID = " Customer";
then I don't understand why you're still getting the error.
As you said originally, you're updating an existing record - not inserting a new one.
Do you still get the error if you temporarily comment out the next line, so that you're making no change at all?
t.FieldValue = "PAYMENT PLAN";
Anthony ClarkePosted Jul 8, 2011, 9:29 AM
I've removed t.FieldID from the update query though and just left t.FieldValue which is not a primary key and i still get the same response.
The only field i want to update is the t.FieldValue is there no way around this?
Thanks in advance Vulpes
Anthony
VulpesPosted Jul 8, 2011, 9:26 AM
If you remove LinkId from the primary key (assuming you don't really need it in there), then that should solve the problem.
Anthony ClarkePosted Jul 8, 2011, 9:21 AM
Ill send you some screenshots in this zip
VulpesPosted Jul 8, 2011, 9:04 AM
EbillzNotesDataContext EB = new EbillzNotesDataContext();
var t = (from p in EB.CustomFieldsDatasEB
where (p.CustomerCode == cuscode) && (p.FieldID == Convert.ToInt32(27))
select p).FirstOrDefault();
t.FieldValue = "PAYMENT PLAN"
t.LinkID = " Customer"
EB.SubmitChanges();
Which field or fields are used in the Primary Key for the table?
Although they don't look like promising candidates, could FieldValue or LinkId be one of them?