Query task

The Query task allows you to retrieve features from a feature layer in an ArcGIS Server map 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 ArcGIS API for Silverlight application. To use a Query task, you need to include code to define its user interface (UI) and specify its execution logic.

An example of XAML and .NET code (in this case C#) for a simple 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 remainder of this topic walks you through how the Query task is defined in the example.

NoteNote:

The code sections in this topic require that the project have references to the ESRI.ArcGIS.Client and System.Runtime.Serialization assemblies. The UserControl of the application's main page (for example, MainPage.xaml) needs to have the following XML namespace declaration:

xmlns:esri="http://schemas.esri.com/arcgis/client/2009"

The code-behind (for example, MainPage.xaml.cs) must include the following using statements:

using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Symbols;

The Grid shown in the following code section replaces the main Grid in the XAML:

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

  <!-- QUERY TASK OUTPUT RESOURCES -->
  <Grid.Resources>
    <esri:SimpleFillSymbol x:Key="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 [STATE_NAME]}" FontWeight="Bold" />
              <StackPanel Orientation="Horizontal">
                <TextBlock Text="Population Density (2007): " />
                <TextBlock Text="{Binding [POP07_SQMI]}" />
              </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>

The methods shown in the following code section follow the constructor in the code-behind:

// Perform query when the 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 the map.
    foreach (Graphic resultFeature in featureSet.Features)
    {
      resultFeature.Symbol = LayoutRoot.Resources["ResultsFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
      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);
}

NoteNote:

Additional examples of the query functionality can be found in the Interactive SDK.

Creating 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 (for example, MainPage.xaml) should look 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="http://schemas.esri.com/arcgis/client/2009">

  <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 (for example, MainPage.xaml.cs) should be unchanged from when you created your application project in Visual Studio.

Creating an input interface for the Query task

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

  1. In the XAML of the application's main page (for example, MainPage.xaml), define a Canvas to hold the task's input interface. Where possible, it's best to use a Canvas as the container element because Silverlight renders these most efficiently.
    <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >      
    </Canvas>
    
  2. Specify a Rectangle to use as the background for the input interface. This rectangle will be semi-transparent and have rounded corners.
    <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>
    
  3. Add a TextBlock to inform the user how to use the task.
    <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>
    
  4. Define a TextBox for specifying the query.
    <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>
    
  5. Add a default query to the TextBox.
    <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>
    
  6. Add a Button to execute the query.
    <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>
    
  7. Specify a handler for the Button's Click event. Later in this topic, you'll implement this handler so that it executes the query.
    <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'll define a GraphicsLayer in the Map element and a SimpleFillSymbol as a static resource. Then, youll specify MapTips on the GraphicsLayer for displaying the results' attributes.

  1. Add a SimpleFillSymbol as a resource in your Silverlight application. The symbol specified here is semi-transparent with a blue fill and outline. Later in this topic, you'll apply this symbol to the task's results in the page's code-behind.
    <Grid.Resources>
      <esri:SimpleFillSymbol x:Key="ResultsFillSymbol" Fill="#500000FF" BorderBrush="Blue" BorderThickness="1" />
    </Grid.Resources>
    
  2. Add a GraphicsLayer to the Map control XAML element.
    TipTip:

    The GraphicsLayer is specified below the map service layer in the XAML so that it's drawn above the map service layer at run time. For further information, see Adding layers.

    <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>
    
  3. Insert a MapTip element within the GraphicsLayer. In 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, use a StackPanel and Border nested inside a Grid as shown in the following code. The background color is specified on the Grid, while the border color is specified on the Border. With the container elements configured this way, the MapTip will automatically resize to fit its contents. In the following steps, you'll configure the MapTip content to displays the current state's name in bold and the state's population density below the state name.
    <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>
    
  4. Add a TextBlock for the state name, and specify a bold font weight.
    <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>
    
  5. For the TextBlock text, specify a data binding expression to bind to the graphic feature's STATE_NAME attribute. 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.
    <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 [STATE_NAME]}" FontWeight="Bold" />
              </StackPanel>
              <Border BorderBrush="Black" BorderThickness="1" />
            </Grid>
          </esri:GraphicsLayer.MapTip>
        </esri:GraphicsLayer>
      </esri:Map.Layers>
    </esri:Map>
    
  6. Add a StackPanel with a horizontal orientation to hold the population density label and value.
    <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 [STATE_NAME]}" FontWeight="Bold" />
                <StackPanel Orientation="Horizontal">
                </StackPanel>
              </StackPanel>
              <Border BorderBrush="Black" BorderThickness="1" />
            </Grid>
          </esri:GraphicsLayer.MapTip>
        </esri:GraphicsLayer>
      </esri:Map.Layers>
    </esri:Map>
    
  7. Add a TextBlock for the population density label.
    <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 [STATE_NAME]}" 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>
    
  8. Add a TextBlock for the population density value, binding to the POP07_SQMI field.
    <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 [STATE_NAME]}" FontWeight="Bold" />
                <StackPanel Orientation="Horizontal">
                  <TextBlock Text="Population Density (2007): " />
                  <TextBlock Text="{Binding [POP07_SQMI]}" />
                </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 UI, you need to define its execution logic. The execution logic can be divided into the following three parts:

  • Task execution
  • Task results display
  • Execution error handling

You'll 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 following steps assume that you are adding code to the Page class in the code-behind file for your Silverlight application's main page (for example, MainPage.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 following steps show how to do this in the code-behind of your application's main page (for example, MainPage.xaml.cs). The task is declared and initialized in the code-behind because tasks alone do not define any UI, but rather encapsulate pieces of execution logic. 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. You declared this handler when you defined the QueryButton control in the page's XAML.
    private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    }
    
  2. In the click handler, declare and instantiate a Query task. Set the server-side feature layer that the task will query by passing the layer's URL to the Query task's constructor. To find the URL, you can use the ArcGIS Services Directory. This example uses the states layer in the ArcGIS Server ESRI_Census_USA map service.
    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");
    }
    
  3. Specify a handler for the task's ExecuteCompleted event. The method specified will be called when the Query task is done executing. You'll implement this handler in the Displaying results section.
    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;
    }
    
  4. Specify a handler for the task's Failed event, which fires when there is a problem executing the query. You'll define this handler in the Handling execution errors section.
    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;
    }
    
  5. Declare and instantiate a Query object. The Query object is used to define the execution parameters for Query tasks.
    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();
    }
    
  6. Since you'll draw the query results on the map, specify that geometry be returned with the results.
    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;
    }
    
  7. Define the fields to return with the query results. Here you'll specify that the query return the state name and population density fields.
    TipTip:

    To minimize network traffic, return only those fields required by your application.

    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" });
    }
    
  8. 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.
    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;
    }
    
  9. Execute the query task.
    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

To display the results, complete the following steps:

  1. Declare a handler for the Query task's ExecuteCompleted event. This handler will be invoked when a query is complete. A FeatureSet containing the features that satisfy the query is passed to the handler's args parameter.
    private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    }
    
  2. Get a reference to the results GraphicsLayer, and clear any previously added graphics from it.
    private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
      GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
      graphicsLayer.ClearGraphics();
    }
    
  3. Check whether any results satisfying the query were found.
    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
      {
      }
    }
    
  4. 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.
    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 = LayoutRoot.Resources["ResultsFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
          graphicsLayer.Graphics.Add(resultFeature);
        }
      }
      else
      {
      }
    }
    
  5. If no features satisfied the query, notify the user with a MessageBox.
    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 = LayoutRoot.Resources["ResultsFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
          graphicsLayer.Graphics.Add(resultFeature);
        }
      }
      else
      {
        MessageBox.Show("No features found");
      }
    }
    

Handling execution errors

Tasks do not always execute as expected, and failures also need to be handled.

  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.
    private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
    {
    }
    
  2. Notify the user of the problem with a MessageBox.
    private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
    {
      MessageBox.Show("Query failed: " + args.Error);
    }
    
9/12/2012