.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Transaction & db.ObjectModified event - problems

1 REPLY 1
Reply
Message 1 of 2
rafal.watroba
435 Views, 1 Reply

Transaction & db.ObjectModified event - problems

Hi,

I want to use Database.ObjectModified event to update attributes in block reference.

1. The following code causes AutoCAD critical error:
Public Shared Sub OnObjectModified(sender,e)
Dim trans As Transaction = CType(sender, DatabaseServices.Database).TransactionManager.StartTransaction
Try
' if object is BlockReference and it's MyBlock -> update attributes
trans.Abort
Finally
trans.Dispose()
End Try
End Sub

I get this error even the handler only start and dispose transaction object.

2. I've used trans.Commit finally and it works. But then I get two errors when I save drawing:
"An unhandled exception of type 'System.NullReferenceException' occurred in acdbmgd.dll"
"An unhandled exception of type 'System.InvalidOperationException' occurred in acdbmgd.dll"

[AutoCAD 2006, VB.NET 2003]

Thanks in advance for your help.
Rafal.
1 REPLY 1
Message 2 of 2
cgay
in reply to: rafal.watroba

rafal,

Hmmm....I remember seeing this error also at one point, but for the life of me I can't quite remember how I fixed it. But you could try something like this.....

For VB.Net 2003......

[code]
Public Shared Sub OnObjectModified(ByVal sender As System.Object, ByVal e As Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs)
If TypeOf sender Is Database Then 'Only try to cast a Database object
Try
Dim db As Database = DirectCast(sender, Database)
Try
Dim trans As Transaction = db.TransactionManager.StartTransaction
Try
' if object is BlockReference and it's MyBlock -> update attributes
trans.Commit() 'Commit if no error
Catch ex As Exception
trans.Abort() 'Abort if there is an error
Finally
trans.Dispose()
End Try
Finally
db.Dispose()
End Try
Catch ex As Exception
Debug.WriteLine(String.Format("Unhandled exception: {0}", ex.ToString))
End Try
Else
Debug.WriteLine(String.Format("OnObjectModified Unhandled Type: {0}", TypeName(sender).ToString))
End If
End Sub
[/code]

For VB.net 2005.......

[code]
Public Shared Sub OnObjectModified(ByVal sender As System.Object, ByVal e As Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs)
If TypeOf sender Is Database Then 'Only try to cast a Database object
'If trycast fails, db will be nothing
Dim db As Database = TryCast(sender, Database)
If db IsNot Nothing Then
Try
''Using' construct calls object.dispose automatically
Using trans As Transaction = db.TransactionManager.StartTransaction
Try
' if object is BlockReference and it's MyBlock -> update attributes
trans.Commit() 'Commit if no error
Catch ex As Exception
trans.Abort() 'Abort if there is an error
End Try
End Using
Finally
db.Dispose()
End Try
End If
Else
Debug.WriteLine(String.Format("OnObjectModified Unhandled Type: {0}", TypeName(sender).ToString))
End If
End Sub
[/code]

Good Luck,
C

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost