Board index » delphi » "Object already in collection" error

"Object already in collection" error

I have had this error, "Object already in collection", come up a few times before.  From my debugging, I believe I have found what incites this error to occur.  For instance, if you use a ADOCommand object to do some housekeeping on a temporary table, like "DELETE FROM RES_TEMP" no error occurs.  But if in the same running instance of the same application, anywhere in the application, if you use an ADOCommand component to do anything else, you get "Object already in collection".  Since Delphi does not use collections, this sounds like something ADODB (Microsoft likes the term "collections") is triggering.  Any ideas out there?

 

Re:"Object already in collection" error


Quote
"Shane Davis" <csda...@cherco.net> wrote in message

news:3b97c571$1_2@dnews...
|
| I have had this error, "Object already in collection", come up a few times
before.  From my debugging, I believe I have found what incites this error
to occur.  For instance, if you use a ADOCommand object to do some
housekeeping on a temporary table, like "DELETE FROM RES_TEMP" no error
occurs.  But if in the same running instance of the same application,
anywhere in the application, if you use an ADOCommand component to do
anything else, you get "Object already in collection".  Since Delphi does
not use collections, this sounds like something ADODB (Microsoft likes the
term "collections") is triggering.  Any ideas out there?

What database are you using?  What commands are triggering the error?
ADO(X) uses collections extensively and it sounds vaguely like you're trying
to illegally modify the db's structure but there's no way of knowing on the
minimal information you've provided.

--

Quidquid latine dictum sit, altum viditur.
#319

Re:"Object already in collection" error


This is a bug in Delphi 5. Here's an old message where Mark Edington
describes how to fix it. I've successfully used this fix.

Regards,
Andy Mackie.

From: Stephen Ahn <s...@visionfps.com.au>
Subject: Re: Error 3367 adErrObjectInCollection - Object Already in
collection. Cannot append.
Date: 22 November 2000 02:11

Fernando J.A. Silva (aka ^Magico^) <mag...@GalaxyCorp.com> wrote in message
news:8vc4ua$1755@bornews.inprise.com...

Quote
> What is causing this error?
> My experience:
> 1 - I open a connection to a SQL Server database
> 2 - Create an instance of a DataModule "A" with a ADOCommand using the
> connection in the first step.
> 3 - Open the ADOCommand (I don't free the instance let them open)
> 4 - Create a new instance of a DataModule "A".
> 5 - Try to open this second instance and the error ocurrs

Fernando,

Borland are aware of this problem :
Previous post from Mark Edington follows :

== start quote ==

Quote
> "Can't append. Object already in collection."
> when I attempt to set a parameter to pass to the stored proc
> at the second instance.

First off, thanks for the attachment.  That makes it really easy for me to
see
the problem and determine what is going on (which I did).

You can work around the problem by commenting out some code in ADODB.pas:

procedure TADOCommand.SetName(const NewName: TComponentName);
begin
  inherited SetName(NewName);
// Comment next two lines to get rid of collection append error.
//  if (NewName <> '') and Assigned(CommandObject) then
//    CommandObject.Name := NewName;
end;

What that code does is give the actual ADO Command object the same name as
the
TADOCommand component.  This allows the command to be executed via variant
dispatch on the ADO Connection object.  If you were using VB it would allow
you
to write code that looks like this:

MyConnection.MyProcedure Param1, Param2

In Delphi it's a bit more awkward looking:

Variant(MyADOConnection.ConnectionObject).MyProcedure(Param1, Param2)

Anyway, the point is there is really no requirement for the name to be set
on
the command object unless you plan to utilize this little advertised and
little
used feature of ADO.

Thanks  for the report.  I'll make sure it gets logged and fixed.

Mark
== end quote ==

The suggested code change works well, but it doesn't seem to have made
it into any of  the releases of adodb.pas .

HTH,
Stephen

Other Threads