title: Loop and List logic
body: <p>I have a program that upon loading the user control, adds graphics to a map layer (You shouldn't have to know much about map stuff to help, I believe the problem is with my loop logic.) </p>
  
  <p>Here is my code:</p>
  
  <pre><code>private void UserControl_Loaded(object sender, RoutedEventArgs e)
  {
      con = new System.Data.SqlClient.SqlConnection();
  
      con.ConnectionString = "Data Source=USSW7DEVWS16\\DEVELOPER;Initial Catalog=acrGIS;Integrated Security=True";
  
      con.Open();
      using (SqlCommand command = new SqlCommand("SELECT * FROM arcObjects", con))
      {
          SqlDataReader reader = command.ExecuteReader();
          while (reader.Read())
          {
              int ID = reader.GetInt32(0);
              Object = reader.GetString(1);
              OComment = reader.GetString(2);
              OStreet = reader.GetString(3);
              OCity = reader.GetString(4);
              OState = reader.GetString(5);
              OZip = reader.GetString(6);
              OSpec = reader.GetString(7);
  
              arcObjects.Add(new arcObject() { Object_Num = Object, Comments = OComment, Street = OStreet, City = OCity, State = OState, Zip = OZip, Spec = OSpec });
          }
  
          con.Close();
          foreach (arcObject objects in arcObjects)
          {
              Locator InitialLocatorTask = new Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US_10/GeocodeServer");
              InitialLocatorTask.AddressToLocationsCompleted += InitialLocatorTask_AddressToLocatonCompleted;
              InitialLocatorTask.Failed += InitialLocatorTask_Failed;
              AddressToLocationsParameters addressParams = new AddressToLocationsParameters();
  
              Dictionary&lt;string, string&gt; address = addressParams.Address;
  
              address.Add("Street", objects.Street);
              address.Add("City", objects.City);
              address.Add("State", objects.State);
              address.Add("Zip", objects.Zip);
  
              InitialLocatorTask.AddressToLocationsAsync(addressParams);
          }
      }
  }
  
  private void InitialLocatorTask_AddressToLocatonCompleted(object sender, ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs args)
  {
      con = new System.Data.SqlClient.SqlConnection();
  
      con.ConnectionString = "Data Source=USSW7DEVWS16\\DEVELOPER;Initial Catalog=acrGIS;Integrated Security=True";
  
      con.Open();
      using (SqlCommand command = new SqlCommand("SELECT * FROM arcObjects", con))
      {
          SqlDataReader reader = command.ExecuteReader();
          while (reader.Read())
          {
              int ID = reader.GetInt32(0);
              Object = reader.GetString(1);
              OComment = reader.GetString(2);
              OStreet = reader.GetString(3);
              OCity = reader.GetString(4);
              OState = reader.GetString(5);
              OZip = reader.GetString(6);
              OSpec = reader.GetString(7);
  
              CandidateListBox.Items.Clear();
  
              List&lt;AddressCandidate&gt; returnedCandidates = args.Results;
  
              foreach (AddressCandidate candidate in returnedCandidates)
              {
                  if (candidate.Score &gt;= 80)
                  {
                      CandidateListBox.Items.Add(candidate.Address);
  
                      if (OSpec == "Meter")
                      {
                          Graphic graphic = new Graphic()
                          {
                              Symbol = AddressLayout.Resources["WaterMeterRenderer"] as ESRI.ArcGIS.Client.Symbols.Symbol,
                              Geometry = candidate.Location
                          };
  
                          string address2 = OCity + ", " + OState + " " + OZip;
  
                          graphic.Attributes.Add("MeterNum", Object);
                          graphic.Attributes.Add("Comment", OComment);
                          graphic.Attributes.Add("Address1", OStreet);
                          graphic.Attributes.Add("Address2", address2);
  
                          if (candidate.Location.SpatialReference == null)
                          {
                              candidate.Location.SpatialReference = new SpatialReference(4326);
                          }
  
                          // if (!candidate.Location.SpatialReference.Equals(MyMap.SpatialReference))
                          //{
                          // if (MyMap.SpatialReference.Equals(new SpatialReference(102100)) &amp;&amp; candidate.Location.SpatialReference.Equals(new SpatialReference(4326)))
                          // graphic.Geometry = _mercator.FromGeographic(graphic.Geometry);
                          // else if (MyMap.SpatialReference.Equals(new SpatialReference(4326)) &amp;&amp; candidate.Location.SpatialReference.Equals(new SpatialReference(102100)))
                          // graphic.Geometry = _mercator.ToGeographic(graphic.Geometry);
                          // else if (MyMap.SpatialReference != new SpatialReference(4326))
                          // {
                          GeometryService geometryService = new  
  GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
  
                          geometryService.ProjectCompleted += (s, a) =&gt;
                          {
                              graphic.Geometry = a.Results[0].Geometry;
                          };
  
                          geometryService.Failed += (s, a) =&gt;
                          {
                              MessageBox.Show("Projection error: " + a.Error.Message);
                          };
  
                          //geometryService.ProjectAsync(new List&lt;Graphic&gt; { graphic }, MyMap.SpatialReference);
                          //    }
                          //}
  
                          FeatureLayer graphicsLayer = MyMap.Layers["WaterMeterLayer"] as FeatureLayer;
                          graphicsLayer.Graphics.Add(graphic);
                          //return;
                      }
  
                      if (OSpec == "Lot")
                      {
                          Graphic graphic = new Graphic()
                          {
                              Symbol = AddressLayout.Resources["LotRenderer"] as ESRI.ArcGIS.Client.Symbols.Symbol,
                              Geometry = candidate.Location
                          };
  
                          string address2 = OCity + ", " + OState + " " + OZip;
  
                          graphic.Attributes.Add("MeterNum", Object);
                          graphic.Attributes.Add("Comment", OComment);
                          graphic.Attributes.Add("Address1", OStreet);
                          graphic.Attributes.Add("Address2", address2);
  
                          if (candidate.Location.SpatialReference == null)
                          {
                              candidate.Location.SpatialReference = new SpatialReference(4326);
                          }
  
                          //if (!candidate.Location.SpatialReference.Equals(MyMap.SpatialReference))
                          // {
                          // if (MyMap.SpatialReference.Equals(new SpatialReference(102100)) &amp;&amp; candidate.Location.SpatialReference.Equals(new SpatialReference(4326)))
                          // graphic.Geometry = _mercator.FromGeographic(graphic.Geometry);
                          // else if (MyMap.SpatialReference.Equals(new SpatialReference(4326)) &amp;&amp; candidate.Location.SpatialReference.Equals(new SpatialReference(102100)))
                          // graphic.Geometry = _mercator.ToGeographic(graphic.Geometry);
                          //    else if (MyMap.SpatialReference != new SpatialReference(4326))
                          //    {
                          GeometryService geometryService = new GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
  
                           geometryService.ProjectCompleted += (s, a) =&gt;
                           {
                               graphic.Geometry = a.Results[0].Geometry;
                           };
  
                           geometryService.Failed += (s, a) =&gt;
                           {
                               MessageBox.Show("Projection error: " + a.Error.Message);
                           };
  
                           //geometryService.ProjectAsync(new List&lt;Graphic&gt; { graphic }, MyMap.SpatialReference);
                           // }
                           // }
  
                           FeatureLayer graphicsLayer = MyMap.Layers["LotLayer"] as FeatureLayer;
                           graphicsLayer.Graphics.Add(graphic);
                           // return;
                       }
                   }
               }
           }
       }
       con.Close();
   }
  
  
  public void InitialLocatorTask_Failed(object sender, TaskFailedEventArgs e)
  {
      MessageBox.Show("Locator service failed: " + e.Error);
  }
  </code></pre>
  
  <p>I know what it happening, the foreach loop in the UserControl_Loaded is adding things to the dictionary and then the while loop in the InitailTaskToAddress_Completed runs over and over again (9 times to be exact). What the problem is, is though while all of the graphics are being placed in the correct place, the attributes of those graphics are all the same... </p>
  
  <p>For some reason it is not getting them. If you want me to clear anything up, just let me know. Also, if you would like to see my XAML, I can also show you that. I am using C# and WPF, as well as arcGIS. Thank you for your help.</p>