BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Preventing Duplicates in BeforePost event.

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (ADO)
View previous topic :: View next topic  
Author Message
Chris Cooper
Guest





PostPosted: Tue Jul 19, 2005 8:58 pm    Post subject: Preventing Duplicates in BeforePost event. Reply with quote



Using D6, SQL2000, ADO.

I have a dataset in BatchUpdate mode and I'm trying to prevent users from
entering duplicat records into a dataset. At first I tried to let the
database report the error with a 'Primary Key' violation and then rollback
the transaction after attempting a BatchUpdate(arAll). However the records
in the dataset which posted correctly, then had a status of UnModified so
the next time I executed the BatchUpdate(arAll) these records would not be
posted to the database (the rollback removed them).

So I went a different route. Using the BeforePost event I cloned the
dataset and tested the value that was about to be posted.

procedure TdmTestDelay.dsetAccessionBeforePost(DataSet: TDataSet);
var CloneSet : TADODataSet;
RS : _RecordSet;
begin
CloneSet := TADODataSet.Create(nil);
try
CloneSet.Connection := dmADOConnections.conANSR;
CloneSet.LockType := ltBatchOptimistic;
RS := TADODataSet(DataSet).Recordset.Clone(adLockUnspecified);
RS.Set_ActiveConnection(nil);
CloneSet.Recordset := RS;
if
CloneSet.Locate('ACCESSION',DataSet.FieldByName('ACCESSION').AsString,[])
then
begin
MessageDlg(DataSet.FieldByName('ACCESSION').AsString+
' has already been entered.', mtInformation, [mbOK], 0);
Abort;
end;
finally
CloneSet.Close;
CloneSet.Free;
end;
end;

This worked great while the user was entering the values. However, if at a
later time in the code a record had to be modified, the BeforePost event
would fire and then I would get the message about a dupicate record. This
is because the cloned dataset now had all entered record and whenever the
BeforePost event fires it aready has the values.

Does anyone else know of a better way to prevent duplicates?


Back to top
Chris Cooper
Guest





PostPosted: Wed Jul 20, 2005 5:05 pm    Post subject: Re: Preventing Duplicates in BeforePost event. Reply with quote



The TADODataSet(DataSet).RecordStatus will aways be rsNew untill the
UpdateBatch is executed. Sorry to be a pain, but using my code can you give
me an example of what you mean?

"Brian Bushay TeamB" <BBushay (AT) Nmpls (DOT) com> wrote


Quote:
This worked great while the user was entering the values. However, if at a
later time in the code a record had to be modified, the BeforePost event
would fire and then I would get the message about a dupicate record. This
is because the cloned dataset now had all entered record and whenever the
BeforePost event fires it aready has the values.
You should be able to check the STATUS property. If it is dsInsert use

your
code and if it is DSUpdate you need to check for more than one record with
your
key value

Quote:

Does anyone else know of a better way to prevent duplicates?

--
Brian Bushay (TeamB)
[email]Bbushay (AT) NMPLS (DOT) com[/email]



Back to top
Chris Cooper
Guest





PostPosted: Wed Jul 20, 2005 5:14 pm    Post subject: Re: Preventing Duplicates in BeforePost event. Reply with quote



Never mind, I realized you ment the state of the dataset not the record. I
think I got it now, thanks for the hint.

"Brian Bushay TeamB" <BBushay (AT) Nmpls (DOT) com> wrote


Quote:
This worked great while the user was entering the values. However, if at a
later time in the code a record had to be modified, the BeforePost event
would fire and then I would get the message about a dupicate record. This
is because the cloned dataset now had all entered record and whenever the
BeforePost event fires it aready has the values.
You should be able to check the STATUS property. If it is dsInsert use

your
code and if it is DSUpdate you need to check for more than one record with
your
key value

Quote:

Does anyone else know of a better way to prevent duplicates?

--
Brian Bushay (TeamB)
[email]Bbushay (AT) NMPLS (DOT) com[/email]



Back to top
Chris Cooper
Guest





PostPosted: Wed Jul 20, 2005 9:21 pm    Post subject: Re: Preventing Duplicates in BeforePost event. Reply with quote

Now I have a new problem. I got the BeforePost working but now the pending
updates will not post to the database. I have verifed that there are indeed
updates pending, and after a call to UpdateBatch there are no more updates
pending, but the records are not in the database.

If I comment out the code in the BeforePost, then the record are posted to
the database. However, for some reason if the code in the BeforePost is
used then no records will be posted to the database on the UpdateBatch.

One more thing is there anyway to debug the UpdateBatch procedure? I would
like to step through it and see some command is not being executed do to
some status. the only procedure I can step into is the following..

procedure TCustomADODataSet.UpdateBatch(AffectRecords: TAffectRecords);
begin
CheckBrowseMode;
Recordset.UpdateBatch(AffectRecordsValues[AffectRecords]); --- Can I step
into this somehow?
UpdateCursorPos;
Resync([]);
end;

Here is my new and inproved BeforePost...

--------------
procedure TdmTestDelay.dsetAccessionBeforePost(DataSet: TDataSet);
var CloneSet : TADODataSet;
RS : _RecordSet;

procedure CloneIt;
begin
CloneSet.Connection := dmADOConnections.conANSR;
CloneSet.LockType := ltBatchOptimistic;
RS := TADODataSet(DataSet).Recordset.Clone(adLockUnspecified);
RS.Set_ActiveConnection(nil);
CloneSet.Recordset := RS;
end;

begin
if not(DataSet.State in [dsInsert, dsEdit]) then exit;
CloneSet := TADODataSet.Create(nil);
try
if (DataSet.State in [dsInsert]) then
begin
CloneIt;
if
CloneSet.Locate('ACCESSION',DataSet.FieldByName('ACCESSION').AsString,[])
then
begin
MessageDlg(DataSet.FieldByName('ACCESSION').AsString+
' has already been entered.', mtInformation, [mbOK], 0);
Abort;
end;
end else
begin
CloneIt;
if
(CloneSet.Locate('ACCESSION',DataSet.FieldByName('ACCESSION').AsString,[]))
and not(CloneSet.RecNo = DataSet.RecNo) then
begin
MessageDlg(DataSet.FieldByName('ACCESSION').AsString+
' has already been entered.', mtInformation, [mbOK], 0);
Abort;
end;
end;
finally
CloneSet.Close;
CloneSet.Free;
end;
end;
-----------------------






"Brian Bushay TeamB" <BBushay (AT) Nmpls (DOT) com> wrote


Quote:
This worked great while the user was entering the values. However, if at a
later time in the code a record had to be modified, the BeforePost event
would fire and then I would get the message about a dupicate record. This
is because the cloned dataset now had all entered record and whenever the
BeforePost event fires it aready has the values.
You should be able to check the STATUS property. If it is dsInsert use

your
code and if it is DSUpdate you need to check for more than one record with
your
key value

Quote:

Does anyone else know of a better way to prevent duplicates?

--
Brian Bushay (TeamB)
[email]Bbushay (AT) NMPLS (DOT) com[/email]



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (ADO) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.