Query Only

Download Samples Repository

Description

Demonstrates how to use a QueryTask to query a map service without a map.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.QueryOnly"
		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/runtime/2013">

	<Grid>
		<Grid.Resources>
			<Style x:Key="TitleTextStyle" TargetType="TextBlock">
				<Setter Property="FontWeight" Value="SemiBold"/>
			</Style>
		</Grid.Resources>
		<Grid.RowDefinitions>
			<RowDefinition Height="Auto" />
			<RowDefinition />
		</Grid.RowDefinitions>

		<Border Padding="10" BorderBrush="White" BorderThickness="2" Margin="120,20" HorizontalAlignment="Left">
			<StackPanel>
				<TextBlock Text="US State Name contains (case sensitive)" FontSize="16" />
				<StackPanel Orientation="Horizontal">

					<TextBox x:Name="StateNameTextBox"
						 Text="New"
						 TextWrapping="NoWrap"
						 Width="400" FontSize="20"
						 AcceptsReturn="true" />
					<Button Content="Do Query"
						Click="QueryButton_Click" Margin="5,0,0,0" />
				</StackPanel>
			</StackPanel>
		</Border>

		<ListView x:Name="itemListView" Grid.Row="1" Margin="120,20" SelectionMode="Single">
			<ListView.ItemTemplate>
				<DataTemplate>
					<Grid Height="110" Margin="6">
						<Grid.ColumnDefinitions>
							<ColumnDefinition Width="Auto"/>
							<ColumnDefinition Width="*"/>
						</Grid.ColumnDefinitions>

						<Border Padding="10" BorderBrush="White" BorderThickness="2" Width="500" >
							<TextBlock Text="{Binding Attributes[STATE_NAME]}" FontSize="40" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" />
						</Border>

						<Grid Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0">
							<Grid.ColumnDefinitions>
								<ColumnDefinition />
								<ColumnDefinition/>
							</Grid.ColumnDefinitions>

							<Grid.RowDefinitions>
								<RowDefinition/>
								<RowDefinition/>
								<RowDefinition/>
								<RowDefinition/>
								<RowDefinition/>
							</Grid.RowDefinitions>

							<TextBlock Text="Sub-region" Grid.Row="0" Grid.Column="0" Style="{StaticResource TitleTextStyle}"/>
							<TextBlock Text="{Binding  Attributes[SUB_REGION]}" Grid.Row="0" Grid.Column="1" Style="{StaticResource TitleTextStyle}" Margin="15,0,0,0" />

							<TextBlock Text="FIPS" Grid.Row="1" Grid.Column="0" Style="{StaticResource TitleTextStyle}"/>
							<TextBlock Text="{Binding  Attributes[STATE_FIPS]}" Grid.Row="1" Grid.Column="1" Style="{StaticResource TitleTextStyle}" Margin="15,0,0,0" />

							<TextBlock Text="Abbr." Grid.Row="2" Grid.Column="0" Style="{StaticResource TitleTextStyle}"/>
							<TextBlock Text="{Binding  Attributes[STATE_ABBR]}" Grid.Row="2" Grid.Column="1" Style="{StaticResource TitleTextStyle}" Margin="15,0,0,0" />

							<TextBlock Text="Pop. 2000" Grid.Row="3" Grid.Column="0" Style="{StaticResource TitleTextStyle}"/>
							<TextBlock Text="{Binding  Attributes[POP2000]}" Grid.Row="3" Grid.Column="1" Style="{StaticResource TitleTextStyle}" Margin="15,0,0,0" />

							<TextBlock Text="Pop. 2007" Grid.Row="4" Grid.Column="0" Style="{StaticResource TitleTextStyle}"/>
							<TextBlock Text="{Binding  Attributes[POP2007]}" Grid.Row="4" Grid.Column="1" Margin="15,0,0,0" Style="{StaticResource TitleTextStyle}"/>
						</Grid>
					</Grid>
				</DataTemplate>
			</ListView.ItemTemplate>
		</ListView>
	</Grid>
</UserControl>
using Esri.ArcGISRuntime.Tasks.Query;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// Demonstrates how to use a QueryTask to query a map service without a map.
	/// </summary>
	/// <title>Query Only</title>
	/// <category>Tasks</category>
	/// <subcategory>Query</subcategory>
	public partial class QueryOnly : UserControl
	{
		public QueryOnly()
		{
			InitializeComponent();
		}

		private async void QueryButton_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				await RunQuery();
			}
			catch (System.Exception ex)
			{
				MessageBox.Show(ex.Message, "Error");
			}
		}

		private async Task RunQuery()
		{
			try
			{
				QueryTask queryTask = new QueryTask(
					new Uri("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"));

				Query query = new Query(StateNameTextBox.Text);
				query.OutFields.Add("*");

				var result = await queryTask.ExecuteAsync(query);
				itemListView.ItemsSource = result.FeatureSet.Features;
			}
			catch (TaskCanceledException) { }
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Error");
			}
		}
	}
}
Feedback on this topic?