<?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 Re : Two transactions within command in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/two-transactions-within-command/m-p/6362220#M35856</link>
    <description>&lt;P&gt;Excellent! Thanks&amp;nbsp;so much for your help!!&lt;/P&gt;</description>
    <pubDate>Thu, 02 Jun 2016 15:22:32 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2016-06-02T15:22:32Z</dc:date>
    <item>
      <title>Two transactions within command</title>
      <link>https://forums.autodesk.com/t5/net-forum/two-transactions-within-command/m-p/6361615#M35854</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having&amp;nbsp;some c# issues which I haven't managed to completely narrow down but think it may be something to do with having two transactions occurring within a command.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code below is to enable a block to be grouped within a new group with&amp;nbsp;a specific name, but I also want the user to have the option to be able to delete all existing groups first.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;[CommandMethod("CG")] // This command will create a group based on the objectid and add the block to the group
public static void CreateGroup() {

        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;
        Transaction tr = db.TransactionManager.StartTransaction();
        
        //Get block table from drawing
        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
       

        PromptEntityOptions peo = new PromptEntityOptions("\nSelect block type");
        peo.SetRejectMessage("Must be a block");
        peo.AddAllowedClass(typeof(BlockReference), false);
        PromptEntityResult per = ed.GetEntity(peo);
        DBDictionary gd = (DBDictionary)tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
        if (per.Status != PromptStatus.OK)
            return;&lt;BR /&gt;
        // Request to delete all groups

        PromptKeywordOptions pko = new PromptKeywordOptions("\nWould you like delete groups?");
        pko.Keywords.Add("Yes");
        pko.Keywords.Add("No");

        var pkr = ed.GetKeywords(pko);

        if (pkr.StringResult == "Yes")
        {
            using (tr)
            {
                
                foreach (DBDictionaryEntry grp in gd)
                {
                    Group group = (Group)tr.GetObject(grp.Value, OpenMode.ForRead);
                    //group.UpgradeOpen();
                    //group.Erase();

                }

                tr.Commit();
            }
        }
	&lt;BR /&gt;	//Create sequentially named group for each block and add each block to the group	
        dynamic bkID = per.ObjectId;
        ObjectIdCollection refid = bkID.BlockTableRecord.GetBlockReferenceIds(true, true);
        using(tr)

        {
            foreach (ObjectId id in refid)
            {
            int i = refid.IndexOf(id);
            string grpName = "PREFIX" + i.ToString("0000");
            Group grp = new Group("GROUP DESCRIPTION", true);
            gd.UpgradeOpen();
            ObjectId grpID = gd.SetAt(grpName, grp);
            tr.AddNewlyCreatedDBObject(grp, true);
            grp.Append(id);
            }
         
            tr.Commit();


            }
        
}&lt;/PRE&gt;&lt;P&gt;The two components of the code (the group delete and group) creation work perfectly when excecuted as their individual commands however when trying to combine the two, it crashes CAD.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm quite new to C# and coding in general so there may well be some appalling errors in there but any help is appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Matt&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jun 2016 09:06:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/two-transactions-within-command/m-p/6361615#M35854</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-06-02T09:06:42Z</dc:date>
    </item>
    <item>
      <title>Re : Two transactions within command</title>
      <link>https://forums.autodesk.com/t5/net-forum/two-transactions-within-command/m-p/6361701#M35855</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You shouldn't start a transaction an use it outside the &lt;A href="https://msdn.microsoft.com/en-us/library/yh598w02.aspx" target="_blank"&gt;using statement&lt;/A&gt; block code.&lt;/P&gt;
&lt;P&gt;You open the BlockTable, the model space BlockTableRecord, the group dictionary and potentially several groups outside the using scope these object won't be disposed with the transaction.&lt;/P&gt;
&lt;P&gt;Then, you cannot re-use the same transaction after you disposed it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The simplest way is to use a single transaction:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("CG")] // This command will create a group based on the objectid and add the block to the group
        public static void CreateGroup()
        {

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;&lt;BR /&gt;            // Start a transaction and a using statement
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                //Get block table from drawing
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);


                PromptEntityOptions peo = new PromptEntityOptions("\nSelect block type");
                peo.SetRejectMessage("Must be a block");
                peo.AddAllowedClass(typeof(BlockReference), false);
                PromptEntityResult per = ed.GetEntity(peo);
                DBDictionary gd = (DBDictionary)tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
                if (per.Status != PromptStatus.OK)
                    return;

                // Request to delete all groups

                PromptKeywordOptions pko = new PromptKeywordOptions("\nWould you like delete groups?");
                pko.Keywords.Add("Yes");
                pko.Keywords.Add("No");

                var pkr = ed.GetKeywords(pko);

                if (pkr.StringResult == "Yes")
                {
                    foreach (DBDictionaryEntry grp in gd)
                    {
                        Group group = (Group)tr.GetObject(grp.Value, OpenMode.ForRead);
                        //group.UpgradeOpen();
                        //group.Erase();

                    }
                }

                //Create sequentially named group for each block and add each block to the group	
                dynamic bkID = per.ObjectId;
                ObjectIdCollection refid = bkID.BlockTableRecord.GetBlockReferenceIds(true, true);
                foreach (ObjectId id in refid)
                {
                    int i = refid.IndexOf(id);
                    string grpName = "PREFIX" + i.ToString("0000");
                    Group grp = new Group("GROUP DESCRIPTION", true);
                    gd.UpgradeOpen();
                    ObjectId grpID = gd.SetAt(grpName, grp);
                    tr.AddNewlyCreatedDBObject(grp, true);
                    grp.Append(id);
                }

                tr.Commit();
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Jun 2016 10:13:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/two-transactions-within-command/m-p/6361701#M35855</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-06-02T10:13:30Z</dc:date>
    </item>
    <item>
      <title>Re : Two transactions within command</title>
      <link>https://forums.autodesk.com/t5/net-forum/two-transactions-within-command/m-p/6362220#M35856</link>
      <description>&lt;P&gt;Excellent! Thanks&amp;nbsp;so much for your help!!&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jun 2016 15:22:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/two-transactions-within-command/m-p/6362220#M35856</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-06-02T15:22:32Z</dc:date>
    </item>
  </channel>
</rss>

