Get field attribute problem

2012
10
Jump to solution
06-20-2012 06:04 AM
DaveCouture
New Contributor III
After selecting a feature through a query, I'd like a message box to pop up with 2 field values. This is what I have (in red):

 Public Sub SelectMapFeaturesByAttributeQuery(ByVal activeView As IActiveView, ByVal featureLayer As IFeatureLayer, ByVal whereClause As System.String)

        If activeView Is Nothing OrElse featureLayer Is Nothing OrElse whereClause Is Nothing Then
            Return
        End If

        Dim featureSelection As IFeatureSelection = TryCast(featureLayer, IFeatureSelection) ' Dynamic Cast

        ' Set up the query
        Dim queryFilter As IQueryFilter = New QueryFilterClass
        queryFilter.WhereClause = whereClause

        ' Invalidate only the selection cache. Flag the original selection
        activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

        ' Perform the selection
        featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

        ' If PID found, Flag the new selection
        If featureSelection.SelectionSet.Count() = 0 Then
            MsgBox("Can't find the PID")
        Else
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)
        End If

        ' Zoom to selected feature, if checkmarked
        If CheckBox1.CheckState = 1 Then
            Dim pUID As New UID
            pUID.Value = "esriArcMapUI.ZoomToSelectedCommand"
            My.ArcMap.Application.Document.CommandBars.Find(pUID).Execute()
        End If


        ' Display Owner's Name and PAN in message box
        Dim cursor As IFeatureCursor
        Dim feature As IFeature
        Dim patspan As String
        Dim taxname As String

        featureSelection.SelectionSet.Search(Nothing, False, cursor)
        feature = cursor.NextFeature
        patspan = feature.Fields.FindField("patspan")
        taxname = feature.Fields.FindField("taxname")

        Do Until feature Is Nothing
            MsgBox("PAN: " + feature.Value(patspan) + vbNewLine + "Owner's Name: " + feature.Value(taxname))
            feature = cursor.NextFeature
        Loop   
 End Sub

End Class
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
You should put your query filter into the Search


cursor = featureLayer.Search(queryFilter, False)

View solution in original post

0 Kudos
10 Replies
BruceNielsen
Occasional Contributor III
The variables 'patspan' and 'taxname' need to be declared as Integer.
0 Kudos
DaveCouture
New Contributor III
Thanks Bruce, that fixed one problem. Now I get the MessageBox, but I get no values. I believe the problem is with the cursor, because it says the cursor is use without being assigned a value, in the following statement:

featureSelection.SelectionSet.Search(Nothing, False, cursor)
0 Kudos
BruceNielsen
Occasional Contributor III
Try changing 'cursor' from IFeatureCursor to ICursor.
0 Kudos
DaveCouture
New Contributor III
I got it to work. 

I changed this:
featureSelection.SelectionSet.Search(Nothing, False, cursor)


with this:
cursor = featureLayer.Search(Nothing, False)


But, I'm getting the values of the first row, not the selected feature.
0 Kudos
BruceNielsen
Occasional Contributor III
Whatever works. I'm still learning too.
0 Kudos
DaveCouture
New Contributor III
haha, it's not really working, because it returns the values of the first row, in the attribute table.  I just want the values of the selected feature.
0 Kudos
KenBuja
MVP Esteemed Contributor
You should put your query filter into the Search


cursor = featureLayer.Search(queryFilter, False)
0 Kudos
DaveCouture
New Contributor III
Gosh, it seems so obvious now..lol  Thanks a bunch, Ken!
0 Kudos
JoeHershman
MVP Regular Contributor
haha, it's not really working, because it returns the values of the first row, in the attribute table.  I just want the values of the selected feature.


In this line of code that 'works' you are searching the entire FeatureClass not the SelectionSet

     cursor = featureLayer.Search(Nothing, False)



I think what you are missing in your previous code is explicitly initializing the ICursor.  I am not a VB guy in C# it would be


    ICursor cursor = null;
    featureSelection.SelectionSet.Search(null, false, cursor)



Something like this in VB


    Dim cursor as ICursor
    cursor = Nothing
    featureSelection.SelectionSet.Search(Nothing, False, cursor)



Hope that helps
Thanks,
-Joe
0 Kudos