How to validate table and field names (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Using geoprocessing > Accessing a dataset > How to validate table and field names (ArcObjects .NET 10.6 SDK)

How to validate table and field names


In this topic


Validating table names

Geodatabases use various relational database management systems (RDBMS) to maintain the many tables that comprise a geodatabase. All tables in a geodatabase must have a unique name, so a mechanism for checking if a table name is unique is essential when creating data in a geodatabase. If you are setting overwrite output to true, there is potential for accidentally overwriting data. Using the ValidateTableName method, a program can determine if a specific name is valid and unique to a specific workspace.
Specifying the workspace as a parameter allows the geoprocessor to check the existing table names and determine if there are naming restrictions imposed by the output workspace. If the output workspace is an RDBMS, it may have reserved words that cannot be used as a table name. It may also have invalid characters that should not be used in a table or field name. ValidateTableName returns a string representing a valid table name that can be the same as the input name if the input name is valid.
The following code example shows how to use ValidateTableName to guarantee that a new output feature class is created by the Copy Features tool:
[C#]
public void ExampleGPValidateTableNames()
{

    // Initialize the geoprocessor.
    Geoprocessor GP=new Geoprocessor();

    // Set the input workspace environment and list all shapefiles.
    GP.SetEnvironmentValue("workspace", @"C:\GPSDK\Samples\ValidateTableName");
    IGpEnumList shapefiles=GP.ListFeatureClasses("*", "", "");

    // Initialize the Copy Features tool.
    CopyFeatures copyFeatures=new CopyFeatures();

    // Output workspace.
    string outputWorkspace=@"Database Connections\pasta1.sde";

    // Get first feature class name.
    string shapefile=shapefiles.Next();

    while (shapefile != "")
    {

        // Validate the output name so it is unique and not empty.
        string outfc=GP.ValidateTableName(shapefile, outputWorkspace);

        // Set input and output feature classes.
        copyFeatures.in_features=shapefile;
        copyFeatures.out_feature_class=outputWorkspace + "\\" + outfc;

        // Execute Copy Features.
        GP.Execute(copyFeatures, null);

        // Get next feature class.
        shapefile=shapefiles.Next();

    }

}
[VB.NET]
Public Sub ExampleGPValidateTableNames()
    
    ' Initialize the geoprocessor.
    Dim GP As Geoprocessor=New Geoprocessor()
    
    ' Set the input workspace environment and list all shapefiles.
    GP.SetEnvironmentValue("workspace", "C:\GPSDK\Samples\ValidateTableName")
    Dim shapefiles As ESRI.ArcGIS.Geoprocessing.IGpEnumList=GP.ListFeatureClasses("*", "", "")
    
    ' Initialize the Copy Features tool.
    Dim copyFeatures As CopyFeatures=New CopyFeatures()
    
    ' Output workspace.
    Dim outputWorkspace As String="Database Connections\pasta1.sde"
    
    ' Get first feature class name.
    Dim shapefile As String=shapefiles.Next()
    
    Do While shapefile <> ""
        
        ' Validate the output name so it is unique and not empty.
        Dim outfc As String=GP.ValidateTableName(shapefile, outputWorkspace)
        
        ' Set input and output feature classes.
        copyFeatures.in_features=shapefile
        copyFeatures.out_feature_class=outputWorkspace & "\" & outfc
        
        ' Execute Copy Features.
        GP.Execute(copyFeatures, Nothing)
        
        ' Get next feature class.
        shapefile=shapefiles.Next()
        
    Loop
    
End Sub

Validating field names

A database can have naming restrictions for field names in a table. Objects, such as feature classes or relationship classes, are stored as tables in an RDBMS, so these restrictions affect more than just stand-alone tables. These restrictions may or may not be common among various database systems, so programs should check all new field names to ensure that a tool does not fail during execution.
The following code example shows how to use the ValidateFieldName method to ensure that a field is added regardless of the input name:
[C#]
public void ExampleGPValidateFieldNames()
{

    // Initialize the geoprocessor.
    Geoprocessor GP=new Geoprocessor();

    // Get the input feature class.
    string inputFeatureClass=@"C:\GP\NFLD.gdb\wells";

    // Get the field name.
    string fieldname="Well-Depth"; // Invalid for a file geodatbase.

    // Validate the field name for entry into a file geodatabase feature class.
    fieldname=GP.ValidateFieldName(fieldname, @"C:\GP\NFLD.gdb");

    // Initialize the AddField tool.
    AddField addfield=new AddField();

    // Set input parameters.
    addfield.in_table=inputFeatureClass;
    addfield.field_name=fieldname;
    addfield.field_type="TEXT";

    // Execute AddField.
    GP.Execute(addfield, null);

}
[VB.NET]
Public Sub ExampleGPValidateFieldNames()
    
    ' Initialize the geoprocessor.
    Dim GP As Geoprocessor=New Geoprocessor()
    
    ' Get the input feature class.
    Dim inputFeatureClass As String="C:\GP\NFLD.gdb\wells"
    
    ' Get the field name.
    Dim fieldname As String="Well-Depth" ' Invalid for a file geodatbase.
    
    ' Validate the field name for entry into a file geodatabase feature class.
    fieldname=GP.ValidateFieldName(fieldname, "C:\GP\NFLD.gdb")
    
    ' Initialize the AddField tool.
    Dim addfield As AddField=New AddField()
    
    ' Set input parameters.
    addfield.in_table=inputFeatureClass
    addfield.field_name=fieldname
    addfield.field_type="TEXT"
    
    ' Execute AddField.
    GP.Execute(addfield, Nothing)
    
End Sub
When a program updates a dataset, such as a feature class or table, be careful to avoid situations where the dataset is locked. For example, Microsoft Access always locks a database for update operations when a table is accessed. Consequently, if you have a personal geodatabase open in ArcCatalog, a program will not be able to update any of the geodatabase contents until it is deselected, the folder is refreshed, or you close ArcCatalog.


See Also:

Geoprocessing considerations for ArcSDE data




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):