dot
Query task

The Query task allows you to retrieve features from a feature layer in an ArcGIS Server map service or a spatially-enabled table in SQL Server via the MapIt Spatial Data Service. Features can be retrieved using spatial and/or attribute query parameters. Once the features are returned, you can use .NET code to display their geometries and attributes in your Silverlight/WPF application. To use a Query task, you will need to include code to define its user interface and specify its execution logic.

An example of XAML and .NET code (in this case C#) for a simple Silverlight application that includes a Query task is shown below. This application defines a Query task that uses a TextBox control for specifying the query and a button for executing the task. Result features are displayed in a GraphicsLayer that has MapTips enabled. The rest of this document will walk you through how the Query task is defined in the example.

[XAML]

<UserControl x:Class="SilverlightApp.Page"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
	xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
	xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client"
	xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client">

	<Grid x:Name="LayoutRoot" Background="White">

		<!-- QUERY TASK OUTPUT RESOURCES -->
		<Grid.Resources>
			<esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
			<esriSymbols:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#500000FF" BorderBrush="Blue"
				BorderThickness="1" />
		</Grid.Resources>

		<!-- MAP -->
		<esri:Map x:Name="MyMap" Extent="-80.05,36.93,-68.79,43.18" >
			<esri:Map.Layers>
				<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
					Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
				<esri:GraphicsLayer ID="MyGraphicsLayer">
					<esri:GraphicsLayer.MapTip>
						<Grid Background="LightYellow">
							<StackPanel>
								<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
									ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
								<StackPanel Orientation="Horizontal">
									<TextBlock Text="Population Density (2007): " />
									<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
										ConverterParameter=POP07_SQMI, Mode=OneWay}" />
								</StackPanel>
							</StackPanel>
							<Border BorderBrush="Black" BorderThickness="1" />
						</Grid>
					</esri:GraphicsLayer.MapTip>
				</esri:GraphicsLayer>
			</esri:Map.Layers>
		</esri:Map>

		<!-- QUERY TASK INTERFACE -->
		<Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
			<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
			<TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
			<TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" Text="POP07_SQMI > 500" />
			<Button x:Name="QueryButton" Content="Execute" Margin="168,23,0,0" Click="QueryButton_Click" />
		</Canvas>
	</Grid>
</UserControl>

[C#]

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.ValueConverters;

namespace SilverlightApp
{
	public partial class Page : UserControl
	{
		public Page() { InitializeComponent(); }

		// Do query when execute button is clicked
		private void QueryButton_Click(object sender, RoutedEventArgs e)
		{
			// Query task initialization
			QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
				"Demographics/ESRI_Census_USA/MapServer/5");
			queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
			queryTask.Failed += QueryTask_Failed;

			// Query task parameters. Return geometry, state, and population density.
			Query query = new Query();
			query.ReturnGeometry = true;
			query.OutFields.AddRange(new string[] { "STATE_NAME", "POP07_SQMI" });

			// Use textbox text as query condition.  
			query.Where = QueryTextBox.Text;

			queryTask.ExecuteAsync(query);
		}

		// Draw results when query is complete
		private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
		{
			// Clear previous results
			GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
			graphicsLayer.ClearGraphics();

			// Check for new results 
			FeatureSet featureSet = args.FeatureSet;
			if (featureSet.Features.Count > 0)
			{
				// Add results to map
				foreach (Graphic resultFeature in featureSet.Features)
				{
					resultFeature.Symbol = ResultsFillSymbol;
					graphicsLayer.Graphics.Add(resultFeature);
				}
			}
			else
			{
				MessageBox.Show("No features found");
			}
		}

		// Notify when query fails
		private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
		{
			MessageBox.Show("Query failed: " + args.Error);
		}
	}
}

How to create a Query task

The following steps assume you have created a Silverlight application with a map and a base layer as described in Creating a Map. The XAML view of your application's main page (e.g. Page.xaml) should be similar to the following:

<UserControl x:Class="SilverlightApp.Page"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
	xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client">

	<Grid x:Name="LayoutRoot" Background="White">

		<!-- MAP -->
		<esri:Map x:Name="MyMap" Extent="-80.05,36.93,-68.79,43.18" >
			<esri:Map.Layers>
				<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
					Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
			</esri:Map.Layers>
		</esri:Map>
	</Grid>
</UserControl>

The code in the main page's code-behind (e.g. Page.xaml.cs) should be unchanged from when you created your Silverlight application project in Visual Studio.

Creating an input interface for the Query task

Since tasks do not define user interfaces, you need to implement an interface for the Query task's input to allow your application's users to execute queries. For this, the example simply includes a TextBox for defining the query and a Button to execute the task.

  1. In the XAML of the applications main page (e.g. Page.xaml), define a Canvas to hold the task's input interface. Where possible, it is best to use a Canvas as the container element because Silverlight renders these most efficiently.
  2. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
    </Canvas>

  3. Specify a Rectangle to use as the background for the input interface. This rectangle will be semi-transparent and have rounded corners.
  4. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
    </Canvas>

  5. Add a TextBlock to inform the user how to use the task.
  6. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
    	<TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    </Canvas>

  7. Define a TextBox for specifying the query.
  8. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
    	<TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    	<TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" />
    </Canvas>

  9. Add a default query to the TextBox.
  10. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
    	<TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    	<TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" Text="POP07_SQMI > 500" />
    </Canvas>

  11. Add a Button to execute the query.
  12. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
    	<TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    	<TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" Text="POP07_SQMI > 500" />
    	<Button x:Name="QueryButton" Content="Execute" Margin="168,23,0,0" />
    </Canvas>

  13. Specify a handler for the Button's Click event. Later, you will implement this handler so that it executes the query.
  14. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
    	<TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    	<TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" Text="POP07_SQMI > 500" />
    	<Button x:Name="QueryButton" Content="Execute" Margin="168,23,0,0" Click="QueryButton_Click" />
    </Canvas>

Creating an output interface for the Query task

To display the results of the Query task, you need to specify an output interface. For displaying the geometry of results, you will define a GraphicsLayer in the Map element and a SimpleFillSymbol as a static resource. Then you will specify MapTips on the GraphicsLayer for displaying results' attributes. The MapTips will use the DictionaryConverter class to enable binding to attributes on the Query task's results.

  1. Add two XML namespaces - one each to map to the ESRI.ArcGIS.Client.ValueConverters and ESRI.ArcGIS.Client.Symbols namespaces in the ESRI.ArcGIS.Client assembly.
  2. <UserControl x:Class="SilverlightApp.Page"
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    	xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
    	xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client"
    	xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client">

  3. Specify a DictionaryConverter as a resource in your Silverlight application. The DictionaryConverter allows you specify binding to a particular Dictionary key.
  4. <Grid.Resources>
    	<esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
    </Grid.Resources>

  5. Add a SimpleFillsymbol as a resource in your Silverlight application. The symbol specified here is semi-transparent with a blue fill and outline. Later, you will apply this symbol to the task's results in the page's code-behind.
  6. <Grid.Resources>
    	<esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
    	<esriSymbols:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#500000FF" BorderBrush="Blue"
    		BorderThickness="1" />
    </Grid.Resources>

  7. Add a GraphicsLayer to the Map control XAML element. Note that the GraphicsLayer is specified below the map service layer in the XAML so that it is drawn above the map service layer at run time. For further information, refer to the Adding layers topic.
  8. <esri:Map x:Name="MyMap" Extent="-80.05,36.93,-68.79,43.18" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  9. Insert a MapTip element within the GraphicsLayer. Inside the MapTip element, specify the background and border for the MapTip's content. For a simple rectangle with a border and margin around the MapTip text, you can use a StackPanel and Border nested inside a Grid as shown below. The background color will be that specified on the Grid, while the border color will be specified on the Border. With the container elements configured this way, the MapTip will automatically resize to fit its contents. In the following steps, ou will configure the MapTip's content so that it displays the current state's name in bold and the state's population density below the state name.
  10. <esri:Map x:Name="MyMap" Extent="-80.05,36.93,-68.79,43.18" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  11. Add a TextBlock for the state name and specify a bold font weight.
  12. <esri:Map x:Name="MyMap" Extent="-80.05,36.93,-68.79,43.18" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel>
    						<TextBlock FontWeight="Bold" />
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  13. For the TextBlock's text, specify a data binding expression to bind to the graphic feature's STATE_NAME attribute. For this, use the DictionaryConverter resource you declared in step two. Within the MapTip element, the DataContext is the Attributes property of the current feature. This property is a Dictionary where the key is a field name and the value is the feature's attribute value for the field. Since the DictionaryConverter enables binding to a key within a Dictionary, and the DataContext within a MapTip element is a Dictionary, DictionaryConverer can be used within a MapTip element to specify data binding on field names.
  14. <esri:Map x:Name="MyMap" Extent="-80.05,36.93,-68.79,43.18" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel>
    						<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
    							ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  15. Add a StackPanel with a horizontal orientation to hold the population density label and value.
  16. <esri:Map x:Name="MyMap" Extent="-80.05,36.93,-68.79,43.18" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel>
    						<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
    							ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
    						<StackPanel Orientation="Horizontal">
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  17. Add a TextBlock for the population density label
  18. <esri:Map x:Name="MyMap" Extent="-80.05,36.93,-68.79,43.18" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel>
    						<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
    							ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
    						<StackPanel Orientation="Horizontal">
    							<TextBlock Text="Population Density (2007): " />
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  19. Add a TextBlock for the population density value, using the DictionaryConverter to bind to the POP07_SQMI field.
  20. <esri:Map x:Name="MyMap" Extent="-80.05,36.93,-68.79,43.18" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel>
    						<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
    							ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
    						<StackPanel Orientation="Horizontal">
    							<TextBlock Text="Population Density (2007): " />
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
    								ConverterParameter=POP07_SQMI, Mode=OneWay}" />
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

Implementing the Query task's execution logic

Now that you've specified the Query task's user interface, you need to define its execution logic. This execution logic can be divided into three parts:

You will implement these components in .NET code contained in the main page's code-behind. This code is linked to the XAML presentation layer by manipulating elements that you declared in XAML with "x:Name" or "ID" attributes and implementing methods that you declared in XAML as event handlers. The steps below assume that you are adding code to the Page class in the code-behind file for your Silverlight application's main page (e.g. Page.xaml.cs). In this example, C# is used.

Executing the task

To execute a query task, you need to instantiate the task, specify the layer that will be queried, wire the task's event handlers, initialize the task's query parameters, and call the task's execution method. The steps below will show you how to do this in the code-behind of your application's main page (e.g. Page.xaml.cs). The task is declared and initialized in the code-behind because tasks alone do not define any user interface, but rather encapsulate pieces of execution logic. In Silverlight, XAML is reserved for an application's presentation layer, while the code-behind is where business logic is implemented.

The code shown in these steps is written in C#.

  1. In the code-behind class of your application's main page, implement a handler for the QueryButton control's click event. Recall that you declared this handler when you defined the QueryButton control in the page's XAML.
  2. private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    }

  3. In the click handler, declare and instantiate a Query task. The constructor accepts a URL to the server-side feature layer on which the query will execute. The URL can reference a feature layer in an ArcGIS Server map service or a table in a SQL Server database served by the MapIt Spatial Data Service. To find the URL, you can use the ArcGIS Services Directory or the MapIt Spatial Data Services Directory. This example uses the states layer in the ArcGIS Server ESRI_Census_USA map service.
  4. private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    	QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/5");
    }

  5. Specify a handler for the task's ExecuteCompleted event. The method specified will be called when the Query task is done executing. You will implement the handler in the "Displaying results" section.
  6. private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    	QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/5");
    	queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    }

  7. Specify a handler for the task's Failed event, which fires when there is a problem executing the query. You will define the handler in the "Handling execute errors" section.
  8. private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    	QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/5");
    	queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    	queryTask.Failed += QueryTask_Failed;
    }

  9. Declare a Query object and instantiate it. The Query object is used to define the execution parameters for Query tasks.
  10. private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    	QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/5");
    	queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    	queryTask.Failed += QueryTask_Failed;
    
    	Query query = new Query();
    }

  11. Since you will draw the query results on the map, specify that the query return geometry with the results.
  12. private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    	QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/5");
    	queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    	queryTask.Failed += QueryTask_Failed;
    
    	Query query = new Query();
    	query.ReturnGeometry = true;
    }

  13. Define the fields to return with the query results. Here you will specify that the query return the state name and population density fields. Note that only the fields required by your application should be returned so that network traffic is minimized.

    private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    	QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/5");
    	queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    	queryTask.Failed += QueryTask_Failed;
    
    	Query query = new Query();
    	query.ReturnGeometry = true;
    	query.OutFields.AddRange(new string[] { "STATE_NAME", "POP07_SQMI" });
    }

  14. Specify the where clause for the query as the text contained in the QueryTextBox control. The where clause defines the conditions that features must satisfy to be returned in the query results.
  15. private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    	QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/5");
    	queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    	queryTask.Failed += QueryTask_Failed;
    
    	Query query = new Query();
    	query.ReturnGeometry = true;
    	query.OutFields.AddRange(new string[] { "STATE_NAME", "POP07_SQMI" });
    	query.Where = QueryTextBox.Text;
    }

  16. Execute the query task.
  17. private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    	QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/5");
    	queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    	queryTask.Failed += QueryTask_Failed;
    
    	Query query = new Query();
    	query.ReturnGeometry = true;
    	query.OutFields.AddRange(new string[] { "STATE_NAME", "POP07_SQMI" });
    	query.Where = QueryTextBox.Text;
    
    	queryTask.ExecuteAsync(query);
    }

Displaying results

  1. Declare a handler for the Query task's ExecuteCompleted event. This handler will be invoked when a query is complete. A FeatureSet containing thefeatures that satisfy the query is passed to the handler's args parameter.
  2. private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    }

  3. Get a reference to the results GraphicsLayer and clear any previously added graphics from it.
  4. private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    }

  5. Check whether any results satisfying the query were found.
  6. private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    
    	if (args.FeatureSet.Features.Count > 0)
    	{
    	}
    	else
    	{
    	}
    }

  7. If results were found, loop through them. Apply the results fill symbol you declared in the page's XAML to each feature. Then add it to the results GraphicsLayer.
  8. private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    
    	if (args.FeatureSet.Features.Count > 0)
    	{
    		foreach (Graphic resultFeature in args.FeatureSet.Features)
    		{
    			resultFeature.Symbol = ResultsFillSymbol;
    			graphicsLayer.Graphics.Add(resultFeature);
    		}
    	}
    	else
    	{
    	}
    }

  9. If no features satsified the query, notify the user with a MessageBox.
  10. private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    
    	if (args.FeatureSet.Features.Count > 0)
    	{
    		foreach (Graphic resultFeature in args.FeatureSet.Features)
    		{
    			resultFeature.Symbol = ResultsFillSymbol;
    			graphicsLayer.Graphics.Add(resultFeature);
    		}
    	}
    	else
    	{
    		MessageBox.Show("No features found");
    	}
    }

Handling execution errors

  1. Declare a handler for the Query task's Failed event. This handler will be invoked if there is a problem with executing a query.
  2. private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
    {
    }

  3. Notify the user of the problem with a MessageBox
  4. private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
    {
    	MessageBox.Show("Query failed: " + args.Error);
    }