How to get graphic attribute values

4804
6
12-10-2012 01:10 PM
DonFreeman
New Contributor
Hi -
I thought I knew this but I guess I don't. Given a graphic with attributes, how do you obtain the value of a specific attribute key?
I have the following:
  foreach (Graphic g in featureSet.Features)
                {
     //Graphic gr = featureSet.Features[0];
                    g.Symbol = LayoutRoot.Resources["LinesSymbol"] as LineSymbol;
                    graphicsLayer.Graphics.Add(g);

     ID = g.Attributes["PAGsde.DBO.TrafficCountNetwork.OBJECTID"] as string;
     
     // How to get at graphic attributes
     //foreach (var a in g.Attributes)
     //{
     //    MessageBox.Show(a.Key + ":   " + a.Value);
     //}
                }


The loop which is commented out reveals that the key "PAGsde.DBO.TrafficCountNetwork.OBJECTID" is a valid key with a value but ID always comes up null. How can I assign the key value to the variable?
Thanks
0 Kudos
6 Replies
DominiqueBroux
Esri Frequent Contributor
I thought I knew this but I guess I don't. Given a graphic with attributes, how do you obtain the value of a specific attribute key?
I have the following:

That should work. Might be there is a blank or a special character in your key.

Could you check with this code which for sure will use the right name:   
// How to get at graphic attributes
foreach (var a in g.Attributes)
{
    //MessageBox.Show(a.Key + ":   " + a.Value);
    if (a.Value != g.Attributes[a.Key])
        MessageBox.Show("Strange error here: " + a.Key);
}
0 Kudos
DonFreeman
New Contributor
That should work. Might be there is a blank or a special character in your key.

Could you check with this code which for sure will use the right name:   
// How to get at graphic attributes
foreach (var a in g.Attributes)
{
    //MessageBox.Show(a.Key + ":   " + a.Value);
    if (a.Value != g.Attributes[a.Key])
        MessageBox.Show("Strange error here: " + a.Key);
}


Thanks Dom. Unfortunately your sample provides no new insight. I have also tried this:
foreach (var a in gr.Attributes)
{
  MessageBox.Show(a.Key + ":   " + a.Value);
  if (a.Key == "PAGsde.DBO.TrafficCountNetwork.OBJECTID")
   {
     ID = a.Value as string;
   }
}

When this loop runs, the if statement finds true one time and executes the assignment, but the value of ID remains null. The messagebox however shows a value of 739. I just don't get it and am totally frustrated. Can you think of anything else?
Thanks
0 Kudos
DominiqueBroux
Esri Frequent Contributor
When this loop runs, the if statement finds true one time and executes the assignment, but the value of ID remains null. The messagebox however shows a value of 739. I just don't get it and am totally frustrated. Can you think of anything else?

Likely your value is not a string so casting it to string returns null.
Try: ID = a.Value.ToString(); instead.
0 Kudos
DonFreeman
New Contributor
Thanks Dom. That solved the problem. I didn't realize there was a difference between the two syntaxes. However the bad news is I was chasing the wrong rabbit. I am trying to fix some code someone else wrote and frankly I don't understand it all that well.  The actual problem has to do with a relationship query. This code :
Int32 MyVar = Convert.ToInt32(g.Attributes[SelectedRoadsTreeView.Tag as string]);

RelationshipParameter relationshipParameters = new RelationshipParameter()
{
  ObjectIds = new int[] { Convert.ToInt32(g.Attributes[SelectedRoadsTreeView.Tag as string]) },
  OutFields = new string[] { "loc_id, count_id, _year, adt" },
  RelationshipId = 0,
  OutSpatialReference = Map.SpatialReference
};
queryTask.ExecuteRelationshipQueryAsync(relationshipParameters);

fails when it runs with a message that there are missing or invalid parameters. The first line verifies that there is a valid objectid (739 in my case). The service that it is accessing is http://gismaps.pagnet.org/arcgis/rest/services/tempTrafficcounts/MapServer/0.

Can you suggest what might be wrong? The problem may be with the service since the original was destroyed and we tried to recreate it. Perhaps I need something structured differently there? Thanks for any help.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Your feature layer doesn't allow querying on related data.

If you look at this working sample : http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer/0  you'll notice the 2 relationships to 'Tops' and 'Fields', and the 'Query Related Records' URL at the botton of the page.

Your service has not these capabilities.

To allow querying on related data, you must defined a Relationship in your geodatabase and your published map document must contain the 2 layers or tables involved in the Relationship class.
0 Kudos
DonFreeman
New Contributor
Your feature layer doesn't allow querying on related data.

If you look at this working sample : http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer/0  you'll notice the 2 relationships to 'Tops' and 'Fields', and the 'Query Related Records' URL at the botton of the page.

Your service has not these capabilities.

To allow querying on related data, you must defined a Relationship in your geodatabase and your published map document must contain the 2 layers or tables involved in the Relationship class.


Thank you, thank you, thank you. After thinking about it more I suspected the problem was with the service. We had all joins and no relates. That simple suggestion took care of it. Problem solved!
0 Kudos