.NET

.NET

Reply
New Member
rafal.watroba
Posts: 1
Registered: ‎01-26-2006
Message 1 of 2 (164 Views)

Transaction & db.ObjectModified event - problems

164 Views, 1 Replies
02-24-2006 05:35 AM
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.
Distinguished Contributor
cgay
Posts: 125
Registered: ‎08-02-2005
Message 2 of 2 (164 Views)

Re: Transaction & db.ObjectModified event - problems

02-24-2006 07:16 AM 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.