title: Autocad C# - On Undo, the object Xdata becomes null
body: <p>This is in AutoCAD 2016</p>
  
  <p>We have a problem with xdata being shown as null on Undo. We use Xdata to track the custom objects/blocks. We have started noticing this after we moved to AutoCAD2016.</p>
  
  <p>I have included an example. Running the command <strong>DrawRectangleWithXdata</strong> will create a polyline with some xdata. You can examine this by running the command <strong>ShowBlockXdata</strong> after creating the object.</p>
  
  <p>Now run Ctrl+Z to undo the DrawRectangleWithXdata command.
  In the Database_ObjectErased event, you can see the Xdata is null.
  Is there anyway I can get this?</p>
  
  <p>Code below:</p>
  
  <pre><code>public partial class MainClass : Autodesk.AutoCAD.Runtime.IExtensionApplication
  {
  
      /// &lt;summary&gt;
      /// Document collection object
      /// &lt;/summary&gt;
      DocumentCollection docCollection = null;
      public void Initialize()
      {
          docCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
          docCollection.DocumentCreated += new DocumentCollectionEventHandler(docCollection_DocumentCreated);
      }
  
      public void Terminate()
      {
  
      }
  
  
  
  
      /// &lt;summary&gt;
      /// Executed whenever a new document is created/opened
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;&lt;/param&gt;
      /// &lt;param name="e"&gt;&lt;/param&gt;
      void docCollection_DocumentCreated(object sender, DocumentCollectionEventArgs e)
      {
          try
          {
              e.Document.Database.ObjectModified += new ObjectEventHandler(Database_ObjectModified);
              e.Document.Database.ObjectErased += new ObjectErasedEventHandler(Database_ObjectErased);
              e.Document.Database.ObjectAppended += new ObjectEventHandler(Database_ObjectAppended);
          }
          catch { }             
      }
  
      private void Database_ObjectModified(object sender, ObjectEventArgs e)
      {
          object xdata = e.DBObject.XData;
      }
  
      private void Database_ObjectErased(object sender, ObjectErasedEventArgs e)
      {
          object xdata = e.DBObject.XData;
      }
  
      private void Database_ObjectAppended(object sender, ObjectEventArgs e)
      {
          object xdata = e.DBObject.XData;
      }
  
  
      /// &lt;summary&gt;
      /// This method registers the Application name for XData
      /// &lt;/summary&gt;
      /// &lt;param name="regAppName"&gt;Application name to be registered&lt;/param&gt;
      public void AddRegAppTableRecord(string regAppName)
      {
          Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
          Editor ed = doc.Editor;
          Database db = doc.Database;
  
          using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
          {
              RegAppTable regAppTable = (RegAppTable)tr.GetObject(db.RegAppTableId, OpenMode.ForRead, false);
              if (!regAppTable.Has(regAppName))
              {
                  regAppTable.UpgradeOpen();
                  RegAppTableRecord ratr = new RegAppTableRecord();
                  ratr.Name = regAppName;
                  regAppTable.Add(ratr);
                  tr.AddNewlyCreatedDBObject(ratr, true);
              }
              tr.Commit();
          }
      }
  
  
      [CommandMethod("ShowBlockXdata", CommandFlags.UsePickSet)]
      public void ShowBlockXdata()
      {
          Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
          var ss = ed.SelectImplied();
          if (ss.Status == PromptStatus.OK)
          {
              using (Transaction tx = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
              {
                  foreach (ObjectId objId in ss.Value.GetObjectIds())
                  {
                      DBObject dbObject = tx.GetObject(objId, OpenMode.ForRead);
                      ed.WriteMessage(dbObject.XData.ToString());
                  }
              }
          }
      }
  
  
  
      [CommandMethod("DrawRectangleWithXdata")]
      public void DrawRectangle()
      {
          //Polyline rectangle
          var p = new Autodesk.AutoCAD.DatabaseServices.Polyline(4);
          p.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
          p.AddVertexAt(0, new Point2d(10, 0), 0, 0, 0);
          p.AddVertexAt(0, new Point2d(0, 10), 0, 0, 0);
          p.AddVertexAt(0, new Point2d(10, 10), 0, 0, 0);
  
          Database db = HostApplicationServices.WorkingDatabase;
          Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
          Editor ed = doc.Editor;
  
          Transaction tr = db.TransactionManager.StartTransaction();
  
          using (tr)
          {
              BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
              btr.AppendEntity(p);
              tr.AddNewlyCreatedDBObject(p, true);
  
              AddRegAppTableRecord("MY_OBJECT");
  
              List&lt;TypedValue&gt; xdata = new List&lt;TypedValue&gt;();
              xdata.Add(new TypedValue(1001, "MY_OBJECT"));
              xdata.Add(new TypedValue(1070, 1234));
  
              ResultBuffer resBuffer = new ResultBuffer(xdata.ToArray());
  
              p.XData = resBuffer;
  
              tr.Commit();
          }
      }
  
  
  }
  </code></pre>