Page 1 of 1

Duplicate Key Entry

Posted: Wed 15 Dec 2010 10:29
by Zero-G.
Hey

I use your latest components of v6 mySQL dotConnect.
On the server, I have set a unique key, which goes over 5 fields.

When the user enters a duplicate article, then the error is thrown, as expectet.
But my question is: how can I remove this article from the Datacontext, so this input will not be tried again!?

THX

Posted: Thu 16 Dec 2010 16:36
by StanislavK
As I can understand, you need to cancel changes made in DataContext so that the next call of SubmitChanges won't fail as well. Am I correct? For this purpose, please use the RejectChanges method:

Code: Select all

Dept newDept = new Dept() { Deptno = 10, Dname = "New Dept", Loc = "Somewhere" };
dataContext.Depts.InsertOnSubmit(newDept);
      
try {
	dataContext.SubmitChanges();
}
catch(Exception ex)
{        
	dataContext.RejectChanges();
}
Here we try to insert a new entity, and if SubmitChanges fails, we call RejectChanges to remove the problem entity from DataContext.

Please tell us if this helps.