Getting the OID from a selected feature

1141
7
Jump to solution
02-04-2013 02:32 PM
RyanHawkins
New Contributor II
I am trying to get the OID of the selected feature to display in textBox2. I am getting an error that says ArcGIS Desktop has encountered a serious application error and is unable to continue. It was strange I got it to work once but I had to continue through an error screen and havent been able to reenact the scenario since. Now it merely crashes everytime. I have made sure a feature is selected as well. I had copied the code in GetSelectedValues() so I am guessing it is how I am initializing the featurelayer. I am new to developing so any suggestions will be greatly appreciated!
      
    public void StartButton_Click(object sender, EventArgs e)
        {
            IApplication app = (IApplication)this.Hook;
            IMxDocument mapDoc = (IMxDocument)app.Document;
            IFeatureLayer featureLayer = (IFeatureLayer)mapDoc.SelectedLayer;
            GetSelectedValues(featureLayer);
         }
      
    private void GetSelectedValues(IFeatureLayer featureLayer)
        {
            IFeatureSelection featureSelection = featureLayer as IFeatureSelection;
            ISelectionSet selectionSet = featureSelection.SelectionSet;

            ICursor cursor;
            selectionSet.Search(null, false, out cursor);

            IRow row = cursor.NextRow();
            textBox2.Text = row.OID.ToString();

            while (cursor != null)
            {
                System.Diagnostics.Debug.WriteLine(row.OID.ToString());
                row = cursor.NextRow();
            }
        }
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
The problem is this:

while (cursor != null)

It needs to be "row" instead of "cursor".

View solution in original post

0 Kudos
7 Replies
RichardFairhurst
MVP Honored Contributor
It looks like you switched to VB Code on the line that reads:

            IFeatureSelection featureSelection = featureLayer as IFeatureSelection;

I believe it should be:

            IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
     
    public void StartButton_Click(object sender, EventArgs e)
        {
            IApplication app = (IApplication)this.Hook;
            IMxDocument mapDoc = (IMxDocument)app.Document;
            IFeatureLayer featureLayer = (IFeatureLayer)mapDoc.SelectedLayer;
            GetSelectedValues(featureLayer);
         }
       
    private void GetSelectedValues(IFeatureLayer featureLayer)
        {
            IFeatureSelection featureSelection = featureLayer as IFeatureSelection;
            ISelectionSet selectionSet = featureSelection.SelectionSet;

            ICursor cursor;
            selectionSet.Search(null, false, out cursor);

            IRow row = cursor.NextRow();
            textBox2.Text = row.OID.ToString();

            while (cursor != null)
            {
                System.Diagnostics.Debug.WriteLine(row.OID.ToString());
                row = cursor.NextRow();
            }
        }
0 Kudos
MarcinDruzgala
Occasional Contributor
It looks like you switched to VB Code on the line that reads:

            IFeatureSelection featureSelection = featureLayer as IFeatureSelection;(example 1)

I believe it should be:

            IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;(example 2)


Well it's a c# syntax. Difference is, in first example if the IFeatureLayer didn't inherit from IFeatureSelection interface the program wouldn't throw an exception just featureSelection would be null. The second example throws an exception which you have to handle in a proper way;)

Ryan Hawkins, your code looks good. Did you try with try/catch blocks to see what is going on in the GetSelectedValues method?


Cheers
MDruzgala
0 Kudos
RyanHawkins
New Contributor II
Thank you for your responses! I really need to start utilizing try catch statements to find problems. It will find the OID's correctly. I did not have a using System.Diagnostics. I added a try catch for GetSelectedValues and am getting a pop up window saying that the botton line of the while is erroring out. For this case, it is row = cursor.NextRow();. This is after I added System.Diagnostics. I added below the previous line textBox3.Text = row.OID.ToString(); and that is where it is having an error. The while loop looks like this now:
while (cursor != null)
{
System.Diagnostics.Debug.WriteLine(row.OID.ToString());
row = cursor.NextRow(); - Previous location of error after adding using System.Diagnostics
textBox3.Text = row.OID.ToString(); -This is where the error is now
}
I am thinking this is a basic concept error that I am having due to my adolescence in programming.
0 Kudos
ArminMueller
New Contributor III
Ryan,

you have to check if OID is available. Do this with IRow.HasOid

Armin
0 Kudos
RyanHawkins
New Contributor II
I have added row.HasOID == true to the while condition but still get the popup. I have tried an If statement surround everything below selectionSet.Search(null, false, out cursor); as well and that didnt work either. Am I placing it in the wrong spot?
            try
            {
                IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
                ISelectionSet selectionSet = featureSelection.SelectionSet;
                ICursor cursor;
                selectionSet.Search(null, false, out cursor);
                IRow row = cursor.NextRow();
                comboBox1.Items.Add(row.OID.ToString());
                textBox2.Text = row.OID.ToString();
                    while (cursor != null && row.HasOID == true)
                    {
                        System.Diagnostics.Debug.WriteLine(row.OID.ToString());
                        row = cursor.NextRow();
                        comboBox1.Items.Add(row.OID.ToString());
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
0 Kudos
NeilClemmons
Regular Contributor III
The problem is this:

while (cursor != null)

It needs to be "row" instead of "cursor".
0 Kudos
RyanHawkins
New Contributor II
I finally realized the while loop was looping for a null for the previous Row. The error was caused by the row.OID not existing. Here is how I fixed this:

row = cursor.NextRow();
while (row != null)
{
System.Diagnostics.Debug.WriteLine(row.OID.ToString());
comboBox1.Items.Add(row.OID.ToString());
row = cursor.NextRow();
}

Thank you for all your help!
0 Kudos