Editing the Attributes of a feature Programmatically

4041
2
09-19-2014 01:23 PM
JessicaLott
New Contributor III

I have a feature layer with features that I would like to update programmatically. Below is my code:

 

 

        private void EditQueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)

        {

            try

            {

                if (txtServiceType.Text.Contains("Water") || txtServiceType.Text.Contains("WATER"))

                {

                    FeatureLayer fl = MyMap.Layers["Meters"] as FeatureLayer;

                    List<int> deletedServiceOrders = new List<int>();

 

                    FeatureSet fs = args.FeatureSet;

 

                    fs.Features[0].Attributes["PWSID"] = txtMeterNumber.Text;

                    fs.Features[0].Attributes["CIS_METR"] = txtAccountNumber.Text;

                    fs.Features[0].Attributes["CUSTYPE"] = txtCustType.Text;

                    fs.Features[0].Attributes["STR"] = txtStreet.Text;

                    fs.Features[0].Attributes["CITY"] = txtCity.Text;

                    fs.Features[0].Attributes["COMMENTS"] = txtName.Text;

 

                   

                    fl.SaveEdits();

                    fl.Update();

                    DeleteServiceOrder.Visibility = Visibility.Collapsed;

                    fl.EndSaveEdits += featurelayerSO_EndSaveEdits;

                    fl.SaveEditsFailed += featurelayerSO_SaveEditsFailed;

                }

            }

            catch (Exception e)

            {

                MessageBox.Show(e.Message);

            }

        }

 

 

Everything is being set correctly and there are no errors, but the values are not actually being saved into ESRI. I am thinking it has something to do with nothing has ever actually be edited? I know I am missing something. Any help would be appreciated!

0 Kudos
2 Replies
MichaelBranscomb
Esri Frequent Contributor

Hi,

I suggest taking a look at the Dev Summit examples: http://www.arcgis.com/home/item.html?id=3f69c79bbd7340b09fad396647a977a5

Specifically the Editing-Stand-Alone-Tables example.

Cheers

Mike

0 Kudos
JaredStewart
New Contributor II

Hi Jessica,

If your feature layer is being hosted with Feature Access on ArcGIS Server, you could try something like this:

private void EditQueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)

{

     try

     {

          if (txtServiceType.Text.Contains("Water") || txtServiceType.Text.Contains("WATER"))

          {

               var myServiceUrl = ""; // Enter your service URL here (Ex: http://myAGS:6080/arcgis/rest/services/myService/FeatureServer/ )

               FeatureSet fs = args.FeatureSet;

             

               var featureString = String.Format(

                   "[{{\"id\":0, \"updates\": [{{" +

                         "\"geometry\":{{\"x\":{0},\"y\":{1}}}," +

                         "\"attributes\":" +

                         "{{" +

                               "\"PWSID\":{2}," +

                               "\"CIS_METR\":{3}," +

                               "\"CUSTYPE\":{4}," +

                               "\"STR\":{5}," +

                               "\"CITY\":{6}," +

                               "\"COMMENTS\":{7}," +

                         "}}" +

                   "}}]}}]",

                   fs.Features[0].geometry.X,

                   fs.Features[0].geometry.Y,

                   txtMeterNumber.Text,

                   txtAccountNumber.Text,

                   txtCustType.Text,

                   txtStreet.Text,

                   txtCity.Text,

                   txtName.Text

             );

             var parameters = "edits=" + HttpUtility.UrlEncode(featureString) + "&f=pjson";

             var wc = new WebClient();

             wc.headers["Content-Type"] = "application/x-www-form-urlencoded";

             wc.Encoding = Encoding.UTF8;

             wc.UploadStringCompleted += (sender, e) =>
                   {
                          if (e.Error == null)
                          {
                              var responseString = e.Result;

                              if (responseString.contains("\"success\": true"))
                              {
                                   MessageBox.Show("Successfully updated.");
                              }
                              else
                              {
                                   MessageBox.Show("ERROR: " + responseString);
                              }
                         }
                         else
                         {
                             MessageBox.Show("ERROR: " + e.Error.Message);
                         }

                    };
               wc.UploadStringAsync(new Uri(myServiceUrl + "/applyEdits"), "POST", parameters);
          }
     }
     catch (Exception e)
     {
          MessageBox.Show(e.Message);
     }

}

The code will probably need some tweaking to fit your needs, but this is roughly what I've been using to update features programmatically.

0 Kudos