.NET

Reply
Valued Contributor
stuartnathan
Posts: 70
Registered: ‎02-17-2005
Message 1 of 7 (984 Views)
Accepted Solution

Operation is not valid due to the current state of the object.

984 Views, 6 Replies
01-05-2012 02:22 PM

my code is:

      Dim AcadDoc As Document = Application.DocumentManager.MdiActiveDocument
      Dim CurDb As Database = AcadDoc.Database
      Dim lt As LayerTable = DirectCast(tr.GetObject(CurDb.LayerTableId, OpenMode.ForRead), LayerTable)

 if fails on last line with error:-

Operation is not valid due to the current state of the object.

 

This works elsewhere.

 

Any ideas as to where to start looking?

My suggestion was going to be that it sounds like the kind of error you would get when trying to access the database from inside an eventhandler, when the database was not yet available.  Not sure what your purpose is, or if this will work for you, but one solution to that problem is to use the EventHandler to send a command to the stack (I use a P-Invoke of ads_queueexpr), which will execute as soon as AutoCAD has finished opening the drawing, and returned to a quiescent state.

*Expert Elite*
Alfred.NESWADBA
Posts: 9,400
Registered: ‎06-29-2007
Message 2 of 7 (981 Views)

Re: Operation is not valid due to the current state of the object.

01-05-2012 02:25 PM in reply to: stuartnathan

Hi,

 

my guess is:

your transaction "tr" is based on another database (and not based on CurDB)

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Valued Contributor
stuartnathan
Posts: 70
Registered: ‎02-17-2005
Message 3 of 7 (971 Views)

Re: Operation is not valid due to the current state of the object.

01-05-2012 03:28 PM in reply to: Alfred.NESWADBA

Actually no.

 

I didn't mean to confuse you by simplifying the code but

      AcadDoc = DocMan.MdiActiveDocument

      CurDb = AcadDoc.Database

are set when the DocumentActivated event is executed.

 

But I will look at my code again to see if this could be correct.

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 4 of 7 (965 Views)

Re: Operation is not valid due to the current state of the object.

01-05-2012 04:49 PM in reply to: stuartnathan

My suggestion was going to be that it sounds like the kind of error you would get when trying to access the database from inside an eventhandler, when the database was not yet available.  Not sure what your purpose is, or if this will work for you, but one solution to that problem is to use the EventHandler to send a command to the stack (I use a P-Invoke of ads_queueexpr), which will execute as soon as AutoCAD has finished opening the drawing, and returned to a quiescent state.

Dave O.                                                                  Sig-Logos32.png
Valued Mentor
KerryBrown
Posts: 276
Registered: ‎11-29-2008
Message 5 of 7 (953 Views)

Re: Operation is not valid due to the current state of the object.

01-06-2012 12:14 AM in reply to: stuartnathan

>> This works elsewhere.

 

>> Any ideas as to where to start looking?


 

I'd be looking at what is different since you used it 'elsewhere'  

... otherwise more information is required.

//-------------------------------------------------------

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Valued Contributor
stuartnathan
Posts: 70
Registered: ‎02-17-2005
Message 6 of 7 (949 Views)

Re: Operation is not valid due to the current state of the object.

01-06-2012 02:11 AM in reply to: chiefbraincloud

Most of my code is in a common Module.

I also have a Command Class that loads forms, which raise events. I had started a transaction in the event and then called the Module code.

Now I start the transaction in the Module and it now works!

 

Thanks - you saved me a lot of time!

 

Valued Contributor
spanqy
Posts: 90
Registered: ‎11-13-2006
Message 7 of 7 (661 Views)

Re: Operation is not valid due to the current state of the object.

04-18-2012 04:30 PM in reply to: stuartnathan

This error happened to me under the following scenario, which I was able to fix:

 

I was iterating through block table records (OpenMode.ForRead) within a "Using" transaction block.  When a block matched certain criteria, I would call btr.UpgradeOpen, apply my modifications, then call tr.Commit.  This worked for the first block that matched the criteria  but not for the NEXT block that did, which failed with "Operation is not valid due to the current state of the object."

 

This was probably because the transaction had already been commited.  So to handle it, I added a boolean flag and set it to true where I had previously call tr.Commit.  I then checked for True AFTER finishing iterating through all block table records, and then called tr.commit.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!