<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Transaction Null Reference and eWasOpenforWrite Error in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/transaction-null-reference-and-ewasopenforwrite-error/m-p/9890386#M17885</link>
    <description>&lt;P&gt;I cannot seem to find a solution to getting the blockreference when inside an AttributeModifiedEventHandler. When using StartTransaction() I get a NullReferenceException and when using StartOpenCloseTransaction() I get a 'eWasOpenforWrite' error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see below for further info and the event handler code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Double clicking a dynamic block and selecting "OK" inside the enhanced attribute editor gives a NullReferenceException at:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using (Transaction transaction1 = transactionManager.StartTransaction())&lt;/LI-CODE&gt;&lt;P&gt;Copying the dynamic block works however as expected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I change: StartTransaction() to:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using (var transaction1 = transactionManager.StartOpenCloseTransaction())&lt;/LI-CODE&gt;&lt;P&gt;The attribute editor then works, but copying the block gives an 'eWasOpenForWrite' error at:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var blockReference = transaction1.GetObject(id, OpenMode.ForRead) as BlockReference;&lt;/LI-CODE&gt;&lt;P&gt;Below is the full EventHandler.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;private void AttributeModifiedEventHandler(object sender, Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs args)
        {
            var transactionManager = currentDatabase.TransactionManager;

            AttributeReference attributeRef;
            string CadHandle = "";
            bool updateDB = false;

            if (args.DBObject is AttributeReference)
            {
                attributeRef = (AttributeReference)args.DBObject;

                using (Transaction transaction1 = transactionManager.StartTransaction())
                {                  
                    var blockTable = transaction1.GetObject(currentDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
                    var blockTableRecord = transaction1.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;

                    foreach (ObjectId id in blockTableRecord)
                    {
                        // check if the current ObjectId is a block reference one
                        if (id.ObjectClass.DxfName == "INSERT")
                        {
                            var blockReference = transaction1.GetObject(id, OpenMode.ForRead) as BlockReference;


                            if (blockReference.Id == attributeRef.OwnerId)
                            {
                                CadHandle = blockReference.Handle.ToString();
                            }
                        };
                    }
                    transaction1.Commit();
                }
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help anyone can give is extremely appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
    <pubDate>Mon, 23 Nov 2020 22:14:34 GMT</pubDate>
    <dc:creator>drobertsY4GL3</dc:creator>
    <dc:date>2020-11-23T22:14:34Z</dc:date>
    <item>
      <title>Transaction Null Reference and eWasOpenforWrite Error</title>
      <link>https://forums.autodesk.com/t5/net-forum/transaction-null-reference-and-ewasopenforwrite-error/m-p/9890386#M17885</link>
      <description>&lt;P&gt;I cannot seem to find a solution to getting the blockreference when inside an AttributeModifiedEventHandler. When using StartTransaction() I get a NullReferenceException and when using StartOpenCloseTransaction() I get a 'eWasOpenforWrite' error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see below for further info and the event handler code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Double clicking a dynamic block and selecting "OK" inside the enhanced attribute editor gives a NullReferenceException at:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using (Transaction transaction1 = transactionManager.StartTransaction())&lt;/LI-CODE&gt;&lt;P&gt;Copying the dynamic block works however as expected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I change: StartTransaction() to:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using (var transaction1 = transactionManager.StartOpenCloseTransaction())&lt;/LI-CODE&gt;&lt;P&gt;The attribute editor then works, but copying the block gives an 'eWasOpenForWrite' error at:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var blockReference = transaction1.GetObject(id, OpenMode.ForRead) as BlockReference;&lt;/LI-CODE&gt;&lt;P&gt;Below is the full EventHandler.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;private void AttributeModifiedEventHandler(object sender, Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs args)
        {
            var transactionManager = currentDatabase.TransactionManager;

            AttributeReference attributeRef;
            string CadHandle = "";
            bool updateDB = false;

            if (args.DBObject is AttributeReference)
            {
                attributeRef = (AttributeReference)args.DBObject;

                using (Transaction transaction1 = transactionManager.StartTransaction())
                {                  
                    var blockTable = transaction1.GetObject(currentDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
                    var blockTableRecord = transaction1.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;

                    foreach (ObjectId id in blockTableRecord)
                    {
                        // check if the current ObjectId is a block reference one
                        if (id.ObjectClass.DxfName == "INSERT")
                        {
                            var blockReference = transaction1.GetObject(id, OpenMode.ForRead) as BlockReference;


                            if (blockReference.Id == attributeRef.OwnerId)
                            {
                                CadHandle = blockReference.Handle.ToString();
                            }
                        };
                    }
                    transaction1.Commit();
                }
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help anyone can give is extremely appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Nov 2020 22:14:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/transaction-null-reference-and-ewasopenforwrite-error/m-p/9890386#M17885</guid>
      <dc:creator>drobertsY4GL3</dc:creator>
      <dc:date>2020-11-23T22:14:34Z</dc:date>
    </item>
    <item>
      <title>Re: Transaction Null Reference and eWasOpenforWrite Error</title>
      <link>https://forums.autodesk.com/t5/net-forum/transaction-null-reference-and-ewasopenforwrite-error/m-p/9891920#M17886</link>
      <description>&lt;P&gt;It looks to me that the purpose of the code in your AttributeModifiedEventHandler() does is to find the handle of attribute owner (blockreference) and assign it to a variable "CadHandle" (it also looks to me that the code in the eventhandler is not "full" - the local variable "CadHandle" is not used after being assigned value)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not understand is why you need to loop through entire ModelSpace for that while the agrs.DBObject has already had all information you need?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can simply:&lt;/P&gt;
&lt;P&gt;private void XXXXXXXHandler(object sender, ObjectEventArgs args)&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; var att = args.DBObject As AttributeReference;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if (att != null)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; var CadHandle=att.OwnerId.Handle.ToString();&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; // do something with CadHandle...&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; // but do not attempt to modify anything in this event handler, in general&lt;/P&gt;
&lt;P&gt;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;</description>
      <pubDate>Tue, 24 Nov 2020 13:20:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/transaction-null-reference-and-ewasopenforwrite-error/m-p/9891920#M17886</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2020-11-24T13:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: Transaction Null Reference and eWasOpenforWrite Error</title>
      <link>https://forums.autodesk.com/t5/net-forum/transaction-null-reference-and-ewasopenforwrite-error/m-p/9892277#M17887</link>
      <description>&lt;P&gt;Thank you Mr. Yuan.&lt;/P&gt;&lt;P&gt;This is my first AutoCAD plugin, so I am very grateful for the help. Your solution is a good clean example of how it should be done.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Nov 2020 15:24:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/transaction-null-reference-and-ewasopenforwrite-error/m-p/9892277#M17887</guid>
      <dc:creator>drobertsY4GL3</dc:creator>
      <dc:date>2020-11-24T15:24:19Z</dc:date>
    </item>
  </channel>
</rss>

