Results 1 to 4 of 4

Thread: DataTable AppendRow Fail

  1. #1
    Member
    Join Date
    2011-12
    Posts
    4
    0 Did you find this post helpful? Yes

    Default DataTable AppendRow Fail

    I've got the problem reduced a bit.

    Code:
     
     public class Main
     {
      [CommandMethod("DUCTTAPERESET")]
      public static void DuctTapeReset() {
       Debug.Listeners.Add(new TextWriterTraceListener("DebugOutput02.txt"));
       string statusMessage = "Main#DuctTapeReset() - " + DateTime.Now.ToLocalTime();
       Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
       Debug.WriteLine(statusMessage);
       Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
       DataTable table = null; 
       using(OpenCloseTransaction myT = db.TransactionManager.StartOpenCloseTransaction()) {
        try {
         DBDictionary NamedObjsDict = (DBDictionary)myT.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite);
         if (NamedObjsDict.Contains(DuctTapeEntry)) {
          table = (DataTable)myT.GetObject(NamedObjsDict.GetAt(DuctTapeEntry), OpenMode.ForWrite);
          statusMessage = "Found table: column count is " + table.NumColumns;
          Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
          Debug.WriteLine(statusMessage);
          if ((table.NumColumns < 4) || (table.NumRows == 0) || (table.GetRowAt(0).Count < 4)) {
           NamedObjsDict.Remove(DuctTapeEntry);
           statusMessage = "Table corrupted: removed it from Named Objects Dictionary. Call this command again to try re-inserting.";
           Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
           Debug.WriteLine(statusMessage);
          }
         } else {
          table = new DataTable();
          table.AppendColumn(CellType.CharPtr, "SourceFile");
          table.AppendColumn(CellType.CharPtr, "Feature");
          table.AppendColumn(CellType.CharPtr, "AttributeFields");
          table.AppendColumn(CellType.CharPtr, "Layer");
          DataCell[] initialVals = new DataCell[4];
          DataCellCollection row = new DataCellCollection();
          for (int n = 0, count = initialVals.Length; n < count; ++n) {
           DataCell cell = new DataCell();
           cell.SetString(string.Empty);
           row.Add(cell);
          }
          table.AppendRow(row, true);
          statusMessage = "New table: first row has " + table.GetRowAt(0).Count + " cells.";
          Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
          Debug.WriteLine(statusMessage);
          NamedObjsDict.SetAt(DuctTapeEntry, table);
          myT.AddNewlyCreatedDBObject(table, true/*add*/);
         } // as now written, new table has initial blank row
        }
        catch(System.Exception err) {
         statusMessage = "Exception: " + err.Message;
         Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
         Debug.WriteLine(statusMessage);
        }
        finally {
         myT.Commit();
         if (table != null) table.Dispose();
         statusMessage = "End reset.";
         Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
         Debug.WriteLine(statusMessage); Debug.Flush();
        }
       } // end using myT
      }
      static readonly string DuctTapeEntry = "DuctTapeConfig";
    }
    Produces the log file results

    Code:
    Main#DuctTapeReset() - 12/21/2011 8:20:43 AM
    New table: first row has 0 cells.
    End reset.
    ...Sorry for the wordy code, but I'm trying to document this as best I can. Why doesn't AppendRow work? The table gets added to the drawing okay, but it's got one row, and that row doesn't have any DataCell contents in it.
    Any ideas?

  2. #2
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,264
    0 Did you find this post helpful? Yes

    Default Re: DataTable AppendRow Fail

    Got it, give a try
    Code:
        public class Main
        {
            [CommandMethod ("READTABLE")]
            public static void ReadTest()
            {
                Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
                Autodesk.AutoCAD.DatabaseServices.DataTable table = null;
    
                using (Transaction myT = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        DBDictionary NamedObjsDict = (DBDictionary)myT.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                        if (NamedObjsDict.Contains(DuctTapeEntry))
                        {
                            table = (DataTable)myT.GetObject(NamedObjsDict.GetAt(DuctTapeEntry), OpenMode.ForRead);
                            for (int n = 0; n < table.NumRows; n++)
                            {
               
                                DataCellCollection row = table.GetRowAt(n);
                                
                                for (int c = 0; c < table.NumColumns; c++)
                                {
                                    DataCell cell = table.GetCellAt(n, c); ;
          
                                    Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nCell Value: " + cell.Value.ToString());
                                }
                                
                            }
                        }
                    }
                    catch (System.Exception err)
                    {
                        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Exception:\n" + err.StackTrace);
    
                    }
                }
            }
            [CommandMethod("DUCTTAPERESET")]
            public static void DuctTapeReset()
            {
                Debug.Listeners.Add(new TextWriterTraceListener("DebugOutput02.txt"));
                string statusMessage = "Main#DuctTapeReset() - " + DateTime.Now.ToLocalTime();
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
                Debug.WriteLine(statusMessage);
                Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
                Autodesk.AutoCAD.DatabaseServices.DataTable table = null;
                //using (OpenCloseTransaction myT = db.TransactionManager.StartOpenCloseTransaction())
                //{
                using (Transaction myT = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        DBDictionary NamedObjsDict = (DBDictionary)myT.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite);
                        if (NamedObjsDict.Contains(DuctTapeEntry))
                        {
                            table = (DataTable)myT.GetObject(NamedObjsDict.GetAt(DuctTapeEntry), OpenMode.ForWrite);
                            statusMessage = "Found table: row count is " + table.NumRows;
                            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
                            Debug.WriteLine(statusMessage);
                        
                            if ((table.NumColumns != 4) || (table.NumRows == 0))
                  
                            {
                                NamedObjsDict.Remove(DuctTapeEntry);
                                statusMessage = "Table corrupted: removed it from Named Objects Dictionary. Call this command again to try re-inserting.";
                                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
                                Debug.WriteLine(statusMessage);
                            }
                            else
                            {
                                DataCellCollection row = new DataCellCollection();
                                for (int n = 0; n < table.NumColumns; n++)
                                {
                                    DataCell cell = new DataCell();
                                    cell.SetString("blah"+ ((n+1)*(n+1)).ToString());
                                    row.Add(cell);
                                }
                                table.InsertRowAt(table.NumRows-1, row, false);
                            }
                        }
                        else
                        {
                            table = new DataTable();
                            table.AppendColumn(CellType.CharPtr, "SourceFile");
                            table.AppendColumn(CellType.CharPtr, "Feature");
                            table.AppendColumn(CellType.CharPtr, "AttributeFields");
                            table.AppendColumn(CellType.CharPtr, "Layer");
                            DataCellCollection row = new DataCellCollection();
                            for (int n = 0; n < table.NumColumns; n++)
                            {
                                DataCell cell = new DataCell();
                                cell.SetString(string.Empty);
                                row.Add(cell);
                            }
    
                            if (table.NumRows == 0)
                            {
                               
                                table.InsertRowAt(0, row, true);
                            }
                            else
                            {
                                //table.AppendRow(row,true);
                                table.InsertRowAt(table.NumRows - 1, row, true);
                            }
                            statusMessage = "New table: first row has " + table.GetRowAt(0).Count + " cells.";
                            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
     
                            Debug.WriteLine(statusMessage);
                            NamedObjsDict.SetAt(DuctTapeEntry, table);
                            myT.AddNewlyCreatedDBObject(table, true/*add*/);
                            table.DowngradeOpen();//<-- important?
                        } // as now written, new table has initial blank row
                    }
                    catch (System.Exception err)
                    {
                        statusMessage = "Exception: " + err.Message;
                        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
                        Debug.WriteLine(statusMessage);
                    }
                    finally
                    {
                        myT.Commit();
                        if (table != null) table.Dispose();
                        statusMessage = "End reset.";
                        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
                        Debug.WriteLine(statusMessage); Debug.Flush();
                    }
                } // end using myT
            }
            static readonly string DuctTapeEntry = "DuctTapeConfig";
        }
    }
    "The whole problem with the world is that fools and fanatics are always
    so certain of themselves, and wiser people so full of doubts."
    Bertrand Russell

  3. #3
    Member
    Join Date
    2011-12
    Posts
    4
    0 Did you find this post helpful? Yes

    Default Re: DataTable AppendRow Fail

    I finally got it to work. It seems the problem was in trying to add rows to a new DataTable before it had been put into the Named Objects Dictionary. If the table is put in first, it accepts the new row as it should. I hadn't understood that.

  4. #4
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,264
    0 Did you find this post helpful? Yes

    Default Re: DataTable AppendRow Fail

    Glad you solved it
    Cheers

    ~'J'~
    "The whole problem with the world is that fools and fanatics are always
    so certain of themselves, and wiser people so full of doubts."
    Bertrand Russell

Similar Threads

  1. Revit fail.
    By Mikkelebrodersen in forum Revit Architecture - General
    Replies: 3
    Last Post: 2010-11-29, 10:29 AM
  2. 3D SUBTRACT fail
    By musunya in forum AutoCAD General
    Replies: 3
    Last Post: 2010-04-04, 03:32 AM
  3. DataSet and DataTable problem
    By Opie in forum Dot Net API
    Replies: 2
    Last Post: 2009-06-03, 01:20 PM
  4. Fail to get commcntrcontroller!
    By jenniferchavez in forum Land Desktop - General
    Replies: 3
    Last Post: 2005-01-25, 03:04 PM
  5. LDT Menuload Fail
    By justin.d.ehart in forum Land Desktop - General
    Replies: 1
    Last Post: 2004-11-19, 05:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •