Select to view content in your preferred language

Relationships

1704
7
04-13-2011 12:10 PM
JoannaLaroussi
Emerging Contributor
I would like to show inside a grid selected street (this works fine) and corresponding to this street alternate names (here I have the problem). I tried to modify Query Related Records sample from the resource center http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#QueryRelatedRecords, but I did not fully succeeded in this task. In this example user can select point on the map by mouse (I am selecting my street by a query) and when the point is displayed in a grid, one may click on its description and display related features.
private void SelectedWellsTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.OldValue != null)
            {
                Graphic g = e.OldValue as Graphic;
                g.UnSelect();
                g.SetZIndex(0);
            }
           
            if (e.NewValue != null)
            {
                Graphic g = e.NewValue as Graphic;
                g.Select();
                g.SetZIndex(1);
               
                //Relationship query
                RelationshipParameter relationshipParameters = new RelationshipParameter()
                {
                    ObjectIds = new int[] {Convert.ToInt32(g.Attributes[SelectedWellsTreeView.Tag as string])},
                    OutFields = new string[] { "OBJECTID, API_NUMBER, ELEVATION, FORMATION, TOP" },
                    RelationshipId = 3,
                    OutSpatialReference = MyMap.SpatialReference
                };
               
                queryTask.ExecuteRelationshipQueryAsync(relationshipParameters);
            }
        }

I would like that my related alternate names display in the same time as selected street without any clicking like above. Here I query streets:
private void createQuery()
        {
//some other code//
case "Lion":
strQueryTaskURL = "my server address/lion_relate/MapServer/0"
strQueryWhereClause = "LAST_Street LIKE '%" + StreetNameTextBox.Text + "%'";
break;

//more code
}
I am trying to change it to:
private void createQuery()
        {
//some other code//
case "Lion":
strQueryTaskURL = "my server address/lion_relate/MapServer/0"
strQueryWhereClause = "LAST_Street LIKE '%" + StreetNameTextBox.Text + "%'";
if (strQueryWhereClause != null)
                    {
    //but I have problem to add relationship here
                    }

break;

//more code
}

Any suggestions how to make this work?
0 Kudos
7 Replies
JenniferNery
Esri Regular Contributor
If I understood your question, you want to fire up a query on related records without mouse click event. Instead you want the result of another to query that checks for where field like '%value%' to return related records as well.

To update this sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#QueryRelatedRecords.

This still requires two queries. Let's assume default: ResultsDisplay.Visibility = System.Windows.Visibility.Visible.
private void MyTextBox_LostFocus(object sender, RoutedEventArgs e)
{
 Query query = new Query() { ReturnIdsOnly = true };
 query.Where = string.Format("LEASE_NAME like '%{0}%'", MyTextBox.Text.Trim());
 query.OutFields.Add("LEASE_NAME");
 queryTask.ExecuteAsync(query);
}

void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
{
 FeatureSet featureSet = args.FeatureSet;
 //Relationship query
 RelationshipParameter relationshipParameters = new RelationshipParameter()
 {
  ObjectIds = featureSet.ObjectIDs.Cast<int>(),
  OutFields = new string[] { "OBJECTID, API_NUMBER, ELEVATION, FORMATION, TOP" },
  RelationshipId = 3,
  OutSpatialReference = MyMap.SpatialReference
 };
 queryTask.ExecuteRelationshipQueryAsync(relationshipParameters);
}
0 Kudos
JoannaLaroussi
Emerging Contributor
Thanks a lot for your suggestions! Yes, I would like to query in the same time my streets and related to them alternate names without any mouse click event.
I added to my code:
private void StreetNameTextBox_LostFocus(object sender, RoutedEventArgs e)
{
Query query = new Query() { ReturnIdsOnly = true };
query.Where = string.Format("LAST_Street like '%{0}%'", StreetNameTextBox.Text.Trim());
query.OutFields.Add("LAST_Street");
queryTask.ExecuteAsync(query);
}

(LAST_Street is the field name in my feature class, which I query in order to return street name; StreetNameTextBox is a text box, where I enter street name to find using query task)

And changed:

private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
//some other code
case "Lion":
       strQueryGraphicsLayer = "LionGraphicsLayer";
break;
// more code

Which only query my Lion streets to:
private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
//some other code
case "Lion":
         strQueryGraphicsLayer = "LionGraphicsLayer";
         FeatureSet newFeatureSet = args.FeatureSet;
         RelationshipParameter relationshipParameters = new RelationshipParameter()
         {
          ObjectIds = newFeatureSet.ObjectIDs.Cast<int>(),
          OutFields = new string[] { "Street" },
          RelationshipId = 1,
          OutSpatialReference = Map.SpatialReference
         };
         queryTask.ExecuteRelationshipQueryAsync(relationshipParameters);
         LionQueryResultPanel.Visibility = System.Windows.Visibility.Visible;
           
         break;
//more code
In order to query Lion streets and alternate names associated with these streets.
(Street is a field name in a related table. I would like that after query LAST_Street, I can see also corresponding errors from Street field)
Here is corresponding part of XAML:
<slData:DataGrid x:Name="RelatedRowsDataGrid" AutoGenerateColumns="False" HeadersVisibility="Column" Background="White"
IsReadOnly="True" Canvas.Left="10" Canvas.Top="50" Height="115" Width="996"
HorizontalScrollBarVisibility="Hidden">
<slData:DataGrid.Columns>
<slData:DataGridTextColumn Width="600" Binding="{Binding Attributes[LAST_Street]}" Header="Street Name"/>
<slData:DataGridTextColumn Width="396" Binding="{Binding  Attributes[Street]}" Header="Alternate Name"/>
</slData:DataGrid.Columns>
</slData:DataGrid>

I also have this part of code from sample:
void QueryTask_ExecuteRelationshipQueryCompleted(object sender, RelationshipEventArgs e)
{
      RelationshipResult pr = e.Result;
          if (pr.RelatedRecordsGroup.Count == 0)
            {
                RelatedRowsDataGrid.ItemsSource = null;
            }
            else
            {
                foreach (var pair in pr.RelatedRecordsGroup)
                {
                    RelatedRowsDataGrid.ItemsSource = pair.Value;
                }
            }
        }

But I am able only to query LAST_Street and relationship query fails with error: �??Query failed: ESRI.ArcGIS.Client.Tasks.ServiseException: Unable to complete operation.�?�
Do you have any suggestions what possible can be wrong with my code?
0 Kudos
JenniferNery
Esri Regular Contributor
This error must have come from the service. You can inspect the Exception from your QueryTask.Failed event handler. Also try to use Fiddler and see if the query parameters seem correct by performing the same query on your web browser (go to the REST service end point).

For example: http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer/0/query?text=...

Using the results of this query, I can perform the query to related table: http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer/2/queryRelate...
0 Kudos
JoannaLaroussi
Emerging Contributor
Thank you for the examples. I was able to check that my queries work just fine on the REST, but the relationship query still doesn�??t work inside my code and I can�??t figure out why. I just opened a ticket with technical support and I hope to hear back soon.
0 Kudos
YingLin
Emerging Contributor
were u able to solve this? I think i am having the same problem. Trying to find an alternative.
Thanks
0 Kudos
JoannaLaroussi
Emerging Contributor
It turned out to be more complicated than it looked at the first glance.
I used this part suggested on the forum:
private void StreetNameTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            Query query = new Query() { ReturnIdsOnly = true };
            query.Where = string.Format("LAST_Street like '%{0}%'", StreetNameTextBox.Text.Trim());
            query.OutFields.Add("LAST_Street");
            queryTask.ExecuteAsync(query);
        }

And on this customer support helped me:
void QueryTask_ExecuteRelationshipQueryCompleted(object sender, RelationshipEventArgs e)
        {
            RelationshipResult pr = e.Result;
           
            if (pr.RelatedRecordsGroup.Count == 0)
            {
                RelatedRowsDataGrid.ItemsSource = null;
            }
            else
            {
                IList<Graphic> y = new List<Graphic>();
                var i = 0;
                foreach (var pair in pr.RelatedRecordsGroup)
                {
                    foreach (var res in pair.Value)
                    {
                        Graphic gra = res as Graphic;
                        y.Insert(i, gra);
                    }
                }
                RelatedRowsDataGrid.ItemsSource = y;  


            }
        }

I hope this can help with your problem.
0 Kudos
YingLin
Emerging Contributor
Thanks very much
I have the following code
if( fs.Count() != 0){
RelationshipParameter rp = new RelationshipParameter()
{
RelationshipId = 1,
OutFields = new string[] { "*" },
OutSpatialReference = Map.SpatialReference,
ObjectIds=fs.ObjectIDs.Cast<int>() //this line returns empty result which I think results in executeRelationship failure

};

However the fs.count returns 1. Not sure why fs.ObjectIDs.Cast<int>() returns empty.
0 Kudos