Announcements

Announcement: We’re aware of an issue affecting starting a new topic from category pages and creating new blog posts. New topics can still be started directly from the relevant board. Learn more here.

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

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

Anonymous
Not applicable
10,279 Views
7 Replies
Message 1 of 8

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

Anonymous
Not applicable

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?

0 Likes
Accepted solutions (1)
10,280 Views
7 Replies
Replies (7)
Message 2 of 8

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

my guess is:

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

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 8

Anonymous
Not applicable

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.

0 Likes
Message 4 of 8

chiefbraincloud
Collaborator
Collaborator
Accepted solution

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
0 Likes
Message 5 of 8

kerry_w_brown
Mentor
Mentor

>> 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.


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 6 of 8

Anonymous
Not applicable

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!

 

0 Likes
Message 7 of 8

MarkSanchezSPEC
Advocate
Advocate

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.

Message 8 of 8

alex_b
Advocate
Advocate

I get this exception with the following code:

 

Database db = HostApplicationServices.WorkingDatabase;
Document doc = cad.DocumentManager.MdiActiveDocument;
using (Database dtb = new Database (false, true))
{
	dtb.ReadDwgFile ("Z:\\Hatch Error 2012.dwg", FileShare.ReadWrite, false, null);
	dtb.CloseInput (true);
	//it stops at the next line with the exception:
	// Operation is not valid due to the current state of the object
	Document Doc = Ap.Application.DocumentManager.GetDocument (dtb);

Any ideas?

0 Likes