TONIFAIRBANKS

Get Edit Feature's Table/Layer Index

Discussion created by TONIFAIRBANKS on Dec 9, 2010
Latest reply on Dec 9, 2010 by vtammineni
When editing a feature (IFeature), is it possible to retreive the Table Index (not just the Table Name) of the table being edited?  I am currently using an ESRI code snippet, Get Index Number from Layer Name Snippet, to determine the index, but when a user has two layers/tables in the map with the same name, the wrong index number is returned to update the PKID field.

Here's a copy of my code:

private void sendEvent(IObject inObject, string mode)
        {
            //Get the logged in domain\UserName
            _currentUserInfo = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
           
            IFeature inFeature = null;
            if (inObject == null)
                return;
            //Convert row to feature (test for feature is null before using - this could be a table update)
            inFeature = inObject as IFeature;
            
            //for each field in this row/feature
            for (int fieldNum = 0; fieldNum < inObject.Fields.FieldCount; fieldNum++)
            {
                //Get table name for this feature
                _currentDataset = inObject.Class as IDataset;
                _currentDatasetNameItems = _currentDataset.Name.Split('.');             
                
                tableName = _currentDatasetNameItems[_currentDatasetNameItems.GetLength(0) - 1];
                fieldObj = inObject.Fields.get_Field(fieldNum);
                fieldName = fieldObj.Name;
                //set field value based on specified method
                        try
                        {
                            switch (fieldName)
                            {
                                case "Updt":
                                    try
                                    {
                                        if (fieldObj.Type == esriFieldType.esriFieldTypeDate)
                                            inObject.set_Value(fieldNum, DateTime.Now);
                                        else if (fieldObj.Type == esriFieldType.esriFieldTypeString)
                                            inObject.set_Value(fieldNum, DateTime.Now.ToString());
                                    }
                                    catch
                                    {
                                    }
                                    break;
                                case "Upid":
                                    try
                                    {
                                        lastEditorName = _currentUserInfo;

                                        if (!String.IsNullOrEmpty(lastEditorName))
                                        {
                                            inObject.set_Value(fieldNum, lastEditorName);
                                        }
                                    }
                                    catch
                                    {
                                    }
                                    break;
                                case "Crdt":
                                    //Use for Create only
                                    try
                                    {
                                        if (mode == "ON_CREATE")
                                        {
                                            if (fieldObj.Type == esriFieldType.esriFieldTypeDate)
                                                inObject.set_Value(fieldNum, DateTime.Now);
                                            else if (fieldObj.Type == esriFieldType.esriFieldTypeString)
                                                inObject.set_Value(fieldNum, DateTime.Now.ToString());
                                        }
                                    }
                                    catch
                                    {
                                    }
                                    break;
                                case "Crid":
                                    //Use for Create only
                                    try
                                    {
                                        if (mode == "ON_CREATE")
                                        {
                                        lastEditorName = _currentUserInfo;

                                            if (!String.IsNullOrEmpty(lastEditorName))
                                            {
                                                inObject.set_Value(fieldNum, lastEditorName);
                                            }
                                        }
                                    }
                                    catch
                                    {
                                    }
                                    break;
                                case "PKID" :
                                case "PKId":
                                case "pkid":
                                case "Pkid":
                                case "PKid":
                                
                                    //Use for Create only
                                    try
                                    {
                                        if (mode == "ON_CREATE")
                                        {
                                  
                                        //getMaxColValue(tableName, fieldName);
                                        //MaxValue(13); //13 is the layer index for RoadClosures

                                            getMaxValue(GetIndexNumberFromLayerName(tableName));
                                            inObject.set_Value(fieldNum, MaxValue +1);
                                            //System.Windows.Forms.MessageBox.Show("TableName: " + tableName + "LayerIndex: " 
                                                //+ GetIndexNumberFromLayerName(tableName) + "MaxValue: "+MaxValue);
                            
                                        }

                                    }
                                    catch (Exception ex)
                                    {
                                        System.Windows.Forms.MessageBox.Show("Try update PKID " + MaxValue + "Message:" + ex.Message);
                                    }
                                    break;

                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Problem in setup." + System.Environment.NewLine + "Message:" + ex.Message, "Attribute Assistant Message");
                        }
                        finally
                        {
                           
                        }
            }
        }
public System.Int32 GetIndexNumberFromLayerName(System.String layerName)
        {
            if (layerName == null)
            {
                return -1;
            }
            // Get the number of layers
            //int numberOfLayers = map.LayerCount;
            int numberOfLayers = _editor.Map.LayerCount;
            
            // Loop through the layers and get the correct layer index
            for (System.Int32 i = 0; i < numberOfLayers; i++)
            {
                //if (layerName == map.get_Layer(i).Name)
                if (layerName == _editor.Map.get_Layer(i).Name)
                
                {

                    // Layer was found
                    return i;
                }
            }
            // No layer was found
            return -1;
        }

        void getMaxValue(int theLayerNum)
        {
            try
            {
                IMxDocument pMxDoc;
                IFeatureLayer2 pFLayer;
                IFeatureClass pFClass;
                IFeatureCursor pCursor;
                IDataStatistics pDataStats = new DataStatistics();
                IStatisticsResults pStatsResults;
                pMxDoc = mxDocument;
                //pFLayer = map.get_Layer(theLayerNum) as IFeatureLayer;
                pFLayer = _editor.Map.get_Layer(theLayerNum) as IFeatureLayer2;
                pFClass = pFLayer.FeatureClass;
                pCursor = pFClass.Search(null, false);
                //pDataStats = new DataStatistics();
                pDataStats.Cursor = pCursor as ICursor;
                pDataStats.Field = "PKID";
                pStatsResults = pDataStats.Statistics;
                MaxValue = pStatsResults.Maximum;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error getMaxValue: " + theLayerNum + " " + ex.Message);

            }
        }

Outcomes