VB.NET - Set target feature class and select by attributes

1544
10
04-18-2011 09:34 AM
MatthewLawton
Occasional Contributor
Hello,

I have had difficutly in the past with VB.NET and ArcObjects, it has never quite clicked for me. However, I am bolstered by the new Add-In technology at release 10. It seems like a friendlier way of working with ArcObjects for a non-programmer like me.

But I have hit a stumbling block pretty early on. I am trying to build a very simpel Add-In that will select a voter precinct polygon feature class via user input. I have successfully built the form in Visual Studio and it appears when I test the tool in ArcMap. I am capturing the user input from the text box on the form, but I don't know what the next step is. How do I select a target feature class (the voter precincts) and pass an attribute query?

Here is what I have so far:

Public Class frmPrecinctSearch

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(txtPrecinctSearch.Text) 'for debugging, shows the textbox value is being captured successfully
    End Sub
End Class


If this is too basic of a question then please direct me to the appropriate documentation where I may discover the solution. I seem to be having difficulty with finding ArcObjects documents or instructions that cover this sort of basic functionality.

Thanks!
0 Kudos
10 Replies
JohnHauck
Occasional Contributor II
Here are a few links that will hopefully help you get started.

1) Developer SDKs: This is a good entry point to the documentation regardless of language.

2) ArcObjects SDK 10 .NET: Here you see serveral tabs for concepts and samples, the core API documentation, ect.

3) From link 2 we will go to: Developing with ArcGIS > Learning ArcObjects > Managing Data > Working with feature data > Querying data > Querying geodatabase tables and Executing spatial queries

Hope this helps!
0 Kudos
MatthewLawton
Occasional Contributor
I'm still having difficulty with this. The links provided are a very high-level overview, but I can't seem to find a way to get started.

I have got my button click function this far:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pMap As IMapDocument
        Dim activeView As IActiveView
        Dim featureLayer As IFeatureLayer
        Dim whereClause As String
        pMap = New MapDocument
        activeView = pMap
        featureLayer = pMap.Layer(0, 0)
        whereClause = "PRECINCT = " + txtPrecinctSearch.Text
        SelectMapFeaturesByAttributeQuery(activeView, featureLayer, whereClause)
    End Sub


The variable txtPrecinctSearch.Text is coming from a user input textbox on the form. There are no errors shown in the code, but I get a fatal exception when I try to run this. I just don't understand what is causing the problem. It really is ridiculous that there is no clear documentation on how to set up the variables for the current map, active map frame, and the selected layer.
0 Kudos
JamesCrandall
MVP Frequent Contributor
I don't know if this is the ONLY problem/cause of your issue, but when querying String/Text type fields, you need to put the WHERE clause in single quotes.  Something like this (I am writting this in VB.net so not sure if replacing "+" with "&" as I have done will affect it):


whereClause = "PRECINCT = '" & txtPrecinctSearch.Text & "'"



james

I'm still having difficulty with this. The links provided are a very high-level overview, but I can't seem to find a way to get started.

I have got my button click function this far:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pMap As IMapDocument
        Dim activeView As IActiveView
        Dim featureLayer As IFeatureLayer
        Dim whereClause As String
        pMap = New MapDocument
        activeView = pMap
        featureLayer = pMap.Layer(0, 0)
        whereClause = "PRECINCT = " + txtPrecinctSearch.Text
        SelectMapFeaturesByAttributeQuery(activeView, featureLayer, whereClause)
    End Sub


The variable txtPrecinctSearch.Text is coming from a user input textbox on the form. There are no errors shown in the code, but I get a fatal exception when I try to run this. I just don't understand what is causing the problem. It really is ridiculous that there is no clear documentation on how to set up the variables for the current map, active map frame, and the selected layer.
0 Kudos
MatthewLawton
Occasional Contributor
Thanks, James. I am still getting the fatal exception when I try to run the program, but adding the single quotes to the where clause is still a good idea.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Thanks, James. I am still getting the fatal exception when I try to run the program, but adding the single quotes to the where clause is still a good idea.



Maybe the exception is coming from somewhere else in your application/code.  I really don't know.  You can:

1.  Put a stop somewhere in that particular funciton and step thru the code line by line and see where it fails.

2.  This is a good idea to do anyway: put in a Try Catch block to trap and show error messages.

Try
  'some code that does stuff here

Catch ex As Exception
    MsgBox(ex.ToString)
End Try
0 Kudos
MatthewLawton
Occasional Contributor
Unfortunately, when I attempt to "Step Into" the function, it just launches ArcMap and goes into debug mode. The "Step Into" and "Step Over" tools become greyed out so I can't even diagnose where the exception is occuring.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Unfortunately, when I attempt to "Step Into" the function, it just launches ArcMap and goes into debug mode. The "Step Into" and "Step Over" tools become greyed out so I can't even diagnose where the exception is occuring.


Actually, that's what I meant: step thru in debug mode by putting a stop somewhere.  You can hit F11 to move to the next line, F5 will continue with the full execution.

Edit: also, Debugging means that it launches ArcMap.  This is expected.
0 Kudos
MatthewLawton
Occasional Contributor
I tried placing the stop at several different points in the function, but regardless I get this error in the Immediate Window:

A first chance exception of type 'System.InvalidCastException' occurred in PrecinctSearch2.dll
A first chance exception of type 'System.InvalidCastException' occurred in PrecinctSearch2.dll
A first chance exception of type 'System.InvalidCastException' occurred in PrecinctSearch2.dll


It seems vague, but I am trying to research what may be causing this.
0 Kudos
JamesCrandall
MVP Frequent Contributor
I tried placing the stop at several different points in the function, but regardless I get this error in the Immediate Window:

A first chance exception of type 'System.InvalidCastException' occurred in PrecinctSearch2.dll
A first chance exception of type 'System.InvalidCastException' occurred in PrecinctSearch2.dll
A first chance exception of type 'System.InvalidCastException' occurred in PrecinctSearch2.dll


It seems vague, but I am trying to research what may be causing this.


Is PrecinctSearch included as a reference in your assembly/project?  Is PrecinctSearch an assembly you have built?

j
0 Kudos