Editing on FeatureLayer

796
8
01-17-2011 05:11 AM
KeremCelik
New Contributor
Hi,

I am trying to update/change a record in my feature layer. It sometimes works, sometimes does not.

Here is the problem:

FeatureLayer feature_layer = Map.Layers["Table"];
feature_layer.Graphics[5].Attributes["name"] = "new_name";

In this code fragment,
In the first line, I get feature layer instance from layers added to the Map (Its name is Table).
In the second line, I want to change 5th record's name attribute with "new_name". This code sometimes works perfectly, but sometimes it does not work : It says that feature_layer.Graphics size is zero, in other words there is no feature in feature layer.

Not : The service (feature layer) have enough number of instance to be run in the server.

If you know how to solve this problem, please help me.

As you can see, I want to update attributes of records, programmatically. If you know a better way to do that, it will be great. 

Thanks in advance.
Kerem.
0 Kudos
8 Replies
DominiqueBroux
Esri Frequent Contributor
When are you trying to update your features? (which event?)

It might happen that the feature layer is not yet initialized when you try to update and so the collection of graphics is not loaded yet.
0 Kudos
KeremCelik
New Contributor
When are you trying to update your features? (which event?)

It might happen that the feature layer is not yet initialized when you try to update and so the collection of graphics is not loaded yet.


Actually, there is no event, do I have to do it by a trigger of an event.

Thanks for your fast reply.

Kerem.
0 Kudos
JenniferNery
Esri Regular Contributor
To edit attributes on your FeatureLayer, you might want to consider using FeatureDataForm or FeatureDataGrid. Here's FeatureDataForm example http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#ToolkitFeatureDataForm, you can look at other samples in "Editing" section.

From your first post, I think that when it finds no features in your layer, the layer may not have finished initializing and updating. If you do your code inside UpdateCompleted event, I'm sure it will find the 5th graphic. http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.FeatureLay...
0 Kudos
KeremCelik
New Contributor
Thank you very much Jennifer, the second advise solved my problem perfectly. Now for all the time I can see my records.

But there is another problem. I am sure that the records are edited by the code fragment I wrote in the first post. But when I query (by Query_Task) on the same layer, I cannot see the changes. But in ArcCatalog or ArcMap I can see the changes I made. Actually when I re-open the application I can see the changes I have made.

But in the same session I cannot see the changes.

I tried the following ways;
I set the AutoSave Attribute of feature layer true
I set the DisableClientCaching Attribute of layer to true.

Not : I can add new graphics, and remove existing graphics, the problem is just editing a graphic/record's Attribute.

I could not identify the problem.

Thanks again.

Kerem.
0 Kudos
JenniferNery
Esri Regular Contributor
The QueryTask also has DisableClientCaching property that you can set to True. http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Tasks.Task....

I suspect that you only set this property on the FeatureLayer, at which case, you should expect the attribute value change reflected in the UpdateCompleted event handler after invoking FeatureLayer.Update() at the end of your code for editing attributes.http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.FeatureLay.... When you check the graphic attribute again in the UpdateCompleted eventhandler.

Calling Update() on FeatureLayer or performing your own QueryTask will both verify that the attribute change had been applied to the server, since both will perform query against the server.
0 Kudos
KeremCelik
New Contributor
Thanks a lot Jennifer, the problem is solved 99%. What the1% is that I cannot see the changes immediately. After 1-2 second I can see the changes are made.

Let me give you the code:
#region Update

private void btn_save_Click(object sender, RoutedEventArgs e)
{
 try
 {
  FeatureLayer feature_layer = (FeatureLayer)main_page.MapInstance.Layers["Table"];
  feature_layer.UpdateCompleted += new EventHandler(feature_layer_UpdateCompleted);
  feature_layer.Update();
 }
 catch (Exception exp)
 {
  MessageBox.Show(exp.Message);
 }
}

private void feature_layer_UpdateCompleted(object sender, EventArgs e)
{
 FeatureLayer feature_layer = (FeatureLayer)main_page.MapInstance.Layers["Table"];

 int selected_index = cb_rows.SelectedIndex;
 feature_layer.Graphics[selected_index].Attributes["id"] = int.Parse(tb_id.Text);
 feature_layer.Graphics[selected_index].Attributes["name"] = tb_name.Text;
 feature_layer.Graphics[selected_index].Attributes["given_date"] = dp_given_date.SelectedDate;
 feature_layer.Graphics[selected_index].Attributes["level"] = (double)nud_level.Value;

 cb_rows.Items.Insert(selected_index, tb_id.Text + " - " + tb_name.Text);
 cb_rows.Items.RemoveAt(selected_index + 1);

 cb_rows.SelectedIndex = selected_index;

 btn_save.IsEnabled = false;
}

#endregion

#region Query

private void cb_rows_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 if (cb_rows.SelectedItem == null)
  return;
 int selected_index = cb_rows.SelectedIndex;

 string table_layer_path = main_page.MainMapLayer + "3";

 QueryTask querytask_on_table = new QueryTask(table_layer_path);

 querytask_on_table.DisableClientCaching = true;
 
 querytask_on_table.ExecuteCompleted += QueryOnTable_ExecuteCompleted;
 querytask_on_table.Failed += QueryTask_Failed;

 ESRI.ArcGIS.Client.Tasks.Query query_on_table = new ESRI.ArcGIS.Client.Tasks.Query();

 query_on_table.Where = "id = '" + cb_rows.SelectedItem.ToString().Split('-')[0].Trim() + "'";

 query_on_table.OutFields.Add("*");
 querytask_on_table.ExecuteAsync(query_on_table);
}

public void QueryOnTable_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
{
 IList<Graphic> query_results = null;

 FeatureSet feature_set = args.FeatureSet;
 if (feature_set != null && feature_set.Features.Count > 0)
  query_results = feature_set.Features;

 if (query_results != null && query_results.Count == 1)
 {
  tb_id.Text = int.Parse(query_results[0].Attributes["id"].ToString()).ToString();
  tb_name.Text = query_results[0].Attributes["name"].ToString();
  dp_given_date.SelectedDate = DateTime.Parse(query_results[0].Attributes["given_date"].ToString());
  nud_level.Value = double.Parse(query_results[0].Attributes["level"].ToString());

  btn_save.IsEnabled = false;
 }
}

#endregion

If you see any problem, please help me about it.

Thanks again for your very helpful information.
0 Kudos
JenniferNery
Esri Regular Contributor
I don't see anything wrong with the code snippet you provided. The delay could be because of the update as this operation requires to re-query the layer. Have you looked into using Feature Data Form? A lot of the attribute editing considerations are handled by this control. Maybe you can compare the time difference in the two solutions. The toolkit is also open source and may be downloaded from codeplex, if you only want to get some idea from the code to improve yours.
0 Kudos
KeremCelik
New Contributor
Thank you very much Jennifer. This information helps a lot.
I will take a look at FeatureDataForm.

Kerem.
0 Kudos