Select to view content in your preferred language

Update attributes in FeatureLayer

1051
9
11-02-2010 07:27 AM
S_Dee
by
Emerging Contributor
I have a featurelayer and want to edit (update) the attributes of the records. How can I do this?
My featurelayer.Graphics is always 0. I can do featurelayer.Graphics.Add(newfeature) and it will add a new feature to my database, no problem there. But how can I retrieve the features, edit them and save them back to the database?
Any help would be appreciated.
0 Kudos
9 Replies
DanielWalton
Frequent Contributor
If you're using AGS 10, use the EditorWidget.
0 Kudos
S_Dee
by
Emerging Contributor
To clarify: I want to edit only the attributes (not geometries, some of my layers don't even have geometries, they are just tables) and I want to do it in code. If somebody has a code example, that would be great.
0 Kudos
AliMirzabeigi
Emerging Contributor
For a given Graphic object you can set its attributes as easy as the following code:
...
Graphic g = featureLayer.Graphics[idx];     // idx = index in GraphicCollection of FeatureLayer for edit
g.Attributes["ATTRIBUTE_NAME"] = NEW_VALUE;
...
0 Kudos
S_Dee
by
Emerging Contributor
I was hoping it would work like that. My problem seems to be a different one. My featureLayer.Graphics.Count == 0 even after initializing the layer. This what I'm trying:

            FeatureLayer fLayer = new FeatureLayer()
            {
                ID = "MyLayer",
                Url = "http://mySercer/ArcGIS/rest/services/CI/FeatureServer/0"
            };
            fLayer.Initialized += new EventHandler<EventArgs>(fLayer_Initialized);
            fLayer.Initialize();

The event handler:

        void fLayer_Initialized(object sender, EventArgs e)
        {
            MessageBox.Show(this.fLayer.Graphics.Count.ToString()); // this is 0
            bool test = fLayer.IsInitialized; // this is true
            Graphic newGraphic = new Graphic();
            fLayer.Graphics.Add(newGraphic); // this works, a new record is added to the database
        }

So, even after initializing the layer, the Graphics collection is empty (but I can add new records). What am I missing?
0 Kudos
DanielWalton
Frequent Contributor
Instead of calling Initialize() directly, just add the layer to your map control.

MyMap.Layers.Add(fLayer);
0 Kudos
S_Dee
by
Emerging Contributor
After adding the FeatureLayer to the map, the Graphics collection is populated after UpdateCompleted fired and I can edit attributes. I can use this as a workaround, but ideally I don't want to add the FeatureLayer to the map, because there's nothing to display in the map.
If I call FeatureLayer.Update() on a FeatureLayer that is not added to the map I get a NullReferenceException, even if it is initialized first.
0 Kudos
AliMirzabeigi
Emerging Contributor
If you don't want to add the layer to your map you should invoke the Update() method of the FeatureLayer after your layer gets initialized. See the code snippet below:
private void fLayer_Initialized(object sender, EventArgs e)
        {
            fLayer.UpdateCompleted += new EventHandler(fLayer_UpdateCompleted);
            fLayer.Update();
        }
private void fLayer_UpdateCompleted(object sender, EventArgs e)
        {
            MessageBox.Show(this.fLayer.Graphics.Count.ToString()); // this is not zero
        }
0 Kudos
EduardoMartinez
New Contributor
This is unresolved? I have looked for the solution to this problem in several posts but i can't find it, what i'm doing wrong? with mode set to snapshot i have no problem, i can add, modify and delete graphics, but when i change to ondemand gives me a NullReferenceException

flProgramas.Url = "http://absoluteurl";
flProgramas.Where = "1=1";
flProgramas.AutoSave = false;
flProgramas.Mode = FeatureLayer.QueryMode.OnDemand;
flProgramas.OutFields = new ESRI.ArcGIS.Client.Tasks.OutFields() { "*" };
flProgramas.Initialized += (s, e) =>
{
                flProgramas.UpdateCompleted += fLayer_UpdateCompleted;
                flProgramas.UpdateFailed += flProgramas_UpdateFailed;
                flProgramas.Update();   <<<<-----------------------------Right here
};
flProgramas.Initialize();
flProgramas.EndSaveEdits += flProgramas_EndSaveEdits;
flProgramas.SaveEditsFailed += flProgramas_SaveEditsFailed;

i'm working with tables, and i need to use ondemand because i have a lot of rows,  thanks in advance
0 Kudos
DanielWalton
Frequent Contributor
OnDemand makes no since for a layer that isn't in a map. I'm guessing that the null reference occurs when the layer looks for the Map extent to determine the envelope parameter to pass on to the server. If you need to manage the collection size in a disconnected layer, use the Where parameter.
0 Kudos