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();
VulpesRank #55 · 29.2K reputationPosted Jul 8, 2011, 6:45 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 8, 2011, 5:04 AM EDT
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?
Anthony Clarke99 reputationPosted Jul 8, 2011, 5:21 AM EDT
Ill send you some screenshots in this zip
VulpesRank #55 · 29.2K reputationPosted Jul 8, 2011, 5:26 AM EDT
If you remove LinkId from the primary key (assuming you don't really need it in there), then that should solve the problem.
Anthony Clarke99 reputationPosted Jul 8, 2011, 5:29 AM EDT
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
VulpesRank #55 · 29.2K reputationPosted Jul 8, 2011, 5:56 AM EDT
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 Clarke99 reputationPosted Jul 8, 2011, 6:04 AM EDT
Yeah, if i comment it out i still seem to get the error message????
VulpesRank #55 · 29.2K reputationPosted Jul 8, 2011, 6:35 AM EDT
t.FieldValue = "PAYMENT PLAN" ;
because 't' would be null.
Just as an experiment, try replacing FirstOrDefault() in the query with Single().
Anthony Clarke99 reputationPosted Jul 8, 2011, 7:06 AM EDT
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
VulpesRank #55 · 29.2K reputationPosted Jul 8, 2011, 7:17 AM EDT
Anyway, pleased you've solved it, whatever the explanation was :)