.NET

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

Transaction & db.ObjectModified event - problems

151 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 (151 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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community