Attribute Query Task Question

1652
11
11-01-2010 10:53 AM
ChristineZeller
New Contributor III
I'm trying to implement the Attribute Query example from the SDK and I can get it working on my machine "as is" but when I change over to a layer that I'm servering it doesn't work. Also if I change the example to just use a different field like STATE_ABBR instead of STATE_NAME I have the same problem....it breaks!  Am I missing a change?

Thanks
Christine
0 Kudos
11 Replies
JenniferNery
Esri Regular Contributor
When you say "it breaks", what error message do you see? Have you tried to use Fiddler to see where it possibly fails?
0 Kudos
ChristineZeller
New Contributor III
Jennifer, thanks for the reply!  Yea it fires when I use the State_Name but like I said if I just switch it to a different field like STATE_ABBR (just two easy changes in the code behind).  It never returns the query and/or zooms to the feature.  Here's the last fiddler.

I've attached a screen shot of my last fiddler result.

It seems like there is some little stupid change I'm missing.  Do I need to change the WHERE?  I not really sure what the where is stating  "query.Where = "1=1";"

Christine
0 Kudos
JenniferNery
Esri Regular Contributor
Oh I see what the problem is now. In this sample http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#AttributeQuery (if you are using this), the query OutFields only include "STATE_NAME".

The service that it's using is http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5 and "STATE_ABBR", while it is a valid field, is not included in the OutFields. You can update this line
query.OutFields.AddRange(new string[] { "STATE_NAME", "STATE_ABBR" });
//or simply use * to return all fields -- query.OutFields.Add("*");
0 Kudos
ChristineZeller
New Contributor III
Oh I see what the problem is now. In this sample   http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#AttributeQuery (if you are using this), the query OutFields only include "STATE_NAME". 

The service that it's using is   http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5 and "STATE_ABBR", while it is a valid field, is not included in the OutFields. You can update this line 
query.OutFields.AddRange(new string[] { "STATE_NAME", "STATE_ABBR" });
//or simply use * to return all fields -- query.OutFields.Add("*");




Jennifer, thanks again for the replies. I am using the sample you pointed to. I was just changing the code like:

// Specify fields to return from initial query
//query.OutFields.AddRange(new string[] { "STATE_ABBR" });


If I try this instead:

query.OutFields.AddRange(new string[] { "STATE_NAME", "STATE_ABBR" });

I get the same results...no return...just sits there


If I try:
query.OutFields.Add("*");

I get the same.



The only other thing I'm changing is

 if ((args.UserState as string) == "initial")
            {
                // Just show on initial load
                QueryComboBox.Items.Add("Select...");

                foreach (Graphic graphic in args.FeatureSet.Features)
                {
                    QueryComboBox.Items.Add(graphic.Attributes["STATE_ABBR"].ToString());


                }

                QueryComboBox.SelectedIndex = 0;
                return;
            }
0 Kudos
JenniferNery
Esri Regular Contributor
Looking at how the Query is formed, it uses Text property which checks against the layer's DisplayField http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Tasks.Quer...

In the given layer, DisplayField is STATE_NAME, therefore the query with STATE_ABBR does not return any results.
http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5

I will update Where property instead of Text property in the SelectionChanged event handler.  http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Tasks.Quer...
 query.Where = string.Format("STATE_ABBR = '{0}'", QueryComboBox.SelectedItem);
ChristineZeller
New Contributor III
Jennifer,

Very cool!  It is now coming clear and with your help I have the sample working.  I'm still slightly confused on adding another parameter to my query.  I can't seem to get the syntax down would I do something like:

query.Where = string.Format("STATE_ABBR = '{0}'", QueryComboBox.SelectedItem) and MED_AGE < 46;

or would it go up in the initialize like

query.Where = "MED_AGE < 33" and "MED_AGE_M > 32";

Both syntax are giving me errors. 

You tired to point me in the right direction with this:
http://forums.arcgis.com/threads/16119-Attribute-Query-on-Muitiple-fields
0 Kudos
JenniferNery
Esri Regular Contributor
Have you used string.Format()? Here's some documentation on how it is used. http://msdn.microsoft.com/en-us/library/system.string.format(v=VS.100).aspx

You can update the Where to the following code:
int min = 32, max = 33;
query.Where = string.Format("STATE_ABBR = '{0}' and MED_AGE < {1}and MED_AGE_M > {2}", QueryComboBox.SelectedItem, min, max);


The best way to test if you are constructing a good SQL Where clause is to try it out on the browser first, by clicking on "Query Layer" link underneath the layer URL.
0 Kudos
ChristineZeller
New Contributor III
Jennifer,

Thanks a bunch for your help!  I really apprecaite it.....you rock!  I was hoping to ask another question.

So the for loop that is populating the combo box is running on a field that doesn't have unique vales for each record.....in the sdk example it is populating state names and each state has a unique name but I'm populating it with call types and the call types repeat.  

So my questing is how can I eliminate duplications  (ex. Call Type = Traffic Stop is listed 15 times in the list and I only want to list it once.


                foreach (Graphic graphic in args.FeatureSet.Features)
                {
                    TypeDescComboBox.Items.Add(graphic.Attributes["TYPEDESC"].ToString());
                }


thanks
Christine.
0 Kudos
JenniferNery
Esri Regular Contributor
You can use Distinct() from System.Linq and set your ComboBox ItemsSource property, provided that it does not already contain items. 
TypeDescComboBox.ItemsSource = (from g in args.FeatureSet.Features
            select g.Attributes["TYPEDESC"]).Distinct();


This is just one way, you can also use a dictionary or list to temporarily hold the attribute values and check whether the attribute value already exist before adding to your ComboBox.
0 Kudos