Select to view content in your preferred language

create a simple table

1342
5
Jump to solution
02-16-2012 02:10 PM
AlexanderWolf
Occasional Contributor
Hello,

I am trying to create a table "out of thin air" without backing it by some file. I want to create a StandaloneTable (or other ITable) with three columns.
The examples I am finding suggest using a featureworkspace which means using a file. I don't want to do that. See here: http://www.codekeep.net/snippets/668235c3-789d-4ec3-8d54-a42ee53f5f01.aspx

I want to simply create a table object and tell it to use 3 columns with specified names. That way I can populate the rows myself and create an x,y data layer from it.

Does the possibility to do what I want exist and if yes, would you please show me either an example or shortly explain?

Thanks,
Alex
0 Kudos
1 Solution

Accepted Solutions
AlexanderWolf
Occasional Contributor
As far as I have found, this is not possible in ArcObjects 10. I have resorted to loading the table from a file by using an oledb workspace.

View solution in original post

0 Kudos
5 Replies
PaulKroseman
Frequent Contributor
Creating an X,Y Event layer from a table probably requires a stored table. If you want the end product to be a shapefile, creating and populating a shapefile would be easier.

I do have code to create a table in memory. Works in 9.3; not tested elsewhere. FinalTable is a global variable of type ITable.

Private Sub createDataTable()

    'This function creates a table in memory to store data
    Dim pFields As IFields
    Dim pFieldsEdit As IFieldsEdit
    Dim pField As IField
    Dim pFieldEdit As IFieldEdit
    
    Set pFields = New Fields
    Set pFieldsEdit = pFields
    pFieldsEdit.FieldCount = 4

    Set pField = New Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Length = 10
        .Name = "OID"
        .Type = esriFieldTypeOID
        .IsNullable = False
    End With
    Set pFieldsEdit.Field(0) = pField

    Set pField = New Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Length = 20
        .Name = "PipeFID"
        .Type = esriFieldTypeString
    End With
    Set pFieldsEdit.Field(1) = pField

    Set pField = New Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Length = 10
        .Name = "ParcelsWI"
        .Type = esriFieldTypeInteger
    End With
    Set pFieldsEdit.Field(2) = pField

    Set pField = New Field
    Set pFieldEdit = pField
    With pFieldEdit
        .Length = 10
        .Name = "ParcelsWIB"
        .Type = esriFieldTypeInteger
    End With
    Set pFieldsEdit.Field(3) = pField

    'Create the table using the created fields
    Dim pRecordSet As IRecordSet
    Set pRecordSet = New RecordSet
    Dim pRSInit As IRecordSetInit
    Set pRSInit = pRecordSet
    pRSInit.CreateTable pFields
    
    Set FinalTable = pRecordSet.table

End Sub


To add data to the table:

Dim pRow As IRow
Set pRow = FinalTable.CreateRow
pRow.Value(pRow.Fields.FindField("PipeFID")) = "myvalue"
pRow.Value(pRow.Fields.FindField("ParcelsWI")) = 5
pRow.Value(pRow.Fields.FindField("ParcelsWIB")) = 2
pRow.Store
0 Kudos
AlexanderWolf
Occasional Contributor
Thank you krosemanp for your answer. This almost works for me using arcobjects 10. I'm receiving a COMException when trying to update the columns of the row I just created. In your code it would be this line:
pRow.Value(pRow.Fields.FindField("PipeFID")) = "myvalue"

The error I receive is:
The workspace is not connected.

Since I have created my own Form and added a mapcontrol I must have overlooked something about setting up a valid workspace or something... what could it be?
Thanks,
Alex
0 Kudos
PaulKroseman
Frequent Contributor
I have nothing relating to workspaces in my code. The code for pRow.Value was written within the ThisDocument area of the mxd file. You may have to create a workspace object or get a reference to your map control's workspace when running ArcObjects code in an add-in, extension, or outside program. I don't have any code on hand to provide for this.
0 Kudos
AlexanderWolf
Occasional Contributor
As far as I have found, this is not possible in ArcObjects 10. I have resorted to loading the table from a file by using an oledb workspace.
0 Kudos
RichWawrzonek
Frequent Contributor
Replying to your own post, saying it can't be done and then marking it as answered is kind of comical.

I'll assume that this is a .NET add-in since you are using ArcGIS 10. Just use the code in the link you provided but instead of opening the feature workspace in the manner described open it as an in memory workspace:

//CREATE THE TABLE IN AN IN MEMORY WORKSPACE
IObjectClassDescription ocDescription = new ObjectClassDescriptionClass();
Type factoryType = Type.GetTypeFromProgID(
    "esriDataSourcesGDB.InMemoryWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
    (factoryType);
IWorkspaceName workspaceName = workspaceFactory.Create("", "in_memory", null, 0);
IName name = (IName)workspaceName;
IFeatureWorkspace fws = (IFeatureWorkspace)name.Open();
string tableName = "tempTable";
ITable table = fws.CreateTable(tableName, fields, ocDescription.ClassExtensionCLSID, null, "");


Now you have an in memory ITable that you can use to create an xy event source. Cast the xy event source to a feature class and then to a feature layer and add it to your map document.
0 Kudos