/// <summary>
        /// Function provides you with two separate work flows based on what happens in the UI. If a connection to 
        /// a SQL server table, the table structure will have been copied into the member variable _fieldNameTypeDict.
        /// If the user has described the field names and field types, we use that instead.
        /// </summary>
        /// <param name="fields"></param>
        private void Fields(List<object> fields)
        {
            //DO NOT CHANGE PROPERTIES FOR THE fieldFID
              Field fieldFID = new Field()
              {
            name = "OBJECTID",
            type = "esriFieldTypeInteger",
            alias = "OBJECTID",
            sqlType = "sqlTypeInteger",
            nullable = false,
            editable = false,
            domain = null,
            defaultValue = null
              };

              fields.Add(fieldFID);

              //The following loops through the dictionary of value pairs required for the feature service attribution
              //table. Using the field type, create the appropriate field type and use the key value as the name of the
              //field. I have listed only field field types, please expand on this for your needs. Listed at the top of
              //this field are the ESRI field types you can use.
              if (_sqlServerTableFieldNameAndTypeDictionary != null)
              {
            foreach (KeyValuePair<string, string> keyValuePair in _sqlServerTableFieldNameAndTypeDictionary)
            {
              Console.WriteLine("Key = {0}, Value = {1}", keyValuePair.Key, keyValuePair.Value);

              if (keyValuePair.Value == "nvarchar")
              {
            FieldString field0 = new FieldString()
            {
              name = keyValuePair.Key,
              type = "esriFieldTypeString",
              alias = keyValuePair.Key,
              sqlType = "sqlTypeNVarchar",
              length = 256,
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(field0);
              }

              if (keyValuePair.Value == "float")
              {
            Field field1 = new Field()
            {
              name = keyValuePair.Key,
              type = "esriFieldTypeDouble",
              alias = keyValuePair.Key,
              sqlType = "sqlTypeFloat",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(field1);
              }

              if (keyValuePair.Value == "int")
              {
            Field fieldInt = new Field()
            {
              name = keyValuePair.Key,
              type = "esriFieldTypeInteger",
              alias = keyValuePair.Key,
              sqlType = "sqlTypeInteger",
              nullable = false,
              editable = false,
              domain = null,
              defaultValue = 0
            };

            fields.Add(fieldInt);
              }

              if (keyValuePair.Value == "char")
              {
            FieldString fieldchar = new FieldString()
            {
              name = keyValuePair.Key,
              type = "esriFieldTypeString",
              alias = keyValuePair.Key,
              sqlType = "sqlTypeNVarchar",
              length = 256,
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(fieldchar);
              }

              if (keyValuePair.Value == "datetime")
              {
            FieldString fieldchar = new FieldString()
            {
              name = keyValuePair.Key,
              type = "esriFieldTypeDate",
              alias = keyValuePair.Key,
              sqlType = "sqlTypeDateTime",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(fieldchar);
              }
            }
              }
              else
              {
            foreach (DataGridViewRow row in dataGridViewFields.Rows)
            {
              if (row.Cells[1].FormattedValue.ToString() == "Text")
              {
            FieldString field0 = new FieldString()
            {
              name = row.Cells[0].FormattedValue.ToString(),
              type = "esriFieldTypeString",
              alias = row.Cells[0].FormattedValue.ToString(),
              sqlType = "sqlTypeNVarchar",
              length = 256,
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(field0);
              }

              if (row.Cells[1].FormattedValue.ToString() == "Float" || row.Cells[1].FormattedValue.ToString() == "Double")
              {
            Field field1 = new Field()
            {
              name = row.Cells[0].FormattedValue.ToString(),
              type = "esriFieldTypeDouble",
              alias = row.Cells[0].FormattedValue.ToString(),
              sqlType = "sqlTypeFloat",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(field1);
              }

              if (row.Cells[1].FormattedValue.ToString() == "Raster")
              {
            Field field1 = new Field()
            {
              name = row.Cells[0].FormattedValue.ToString(),
              type = "esriFieldTypeRaster",
              alias = row.Cells[0].FormattedValue.ToString(),
              sqlType = "sqlTypeFloat", //??? Todo
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(field1);
              }

              if (row.Cells[1].FormattedValue.ToString() == "Short" || row.Cells[1].FormattedValue.ToString() == "Long")
              {
            Field fieldInt = new Field()
            {
              name = row.Cells[0].FormattedValue.ToString(),
              type = "esriFieldTypeInteger",
              alias = row.Cells[0].FormattedValue.ToString(),
              sqlType = "sqlTypeInteger",
              nullable = false,
              editable = false,
              domain = null,
              defaultValue = 0
            };

            fields.Add(fieldInt);
              }

              if (row.Cells[1].FormattedValue.ToString() == "Date")
              {
            FieldString fieldchar = new FieldString()
            {
              name = row.Cells[0].FormattedValue.ToString(),
              type = "esriFieldTypeDate",
              alias = row.Cells[0].FormattedValue.ToString(),
              sqlType = "sqlTypeDateTime",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(fieldchar);
              }
            }
              }
        }
        private void AddDefinitionToFeatureService()
        {
            /*
               * The main demonstration code of this project.
               * Iterates through the DataGridView records, creating the fields and ensuring that the esri field types for each are correct.
               * */
              //Item item = null;
              Extent extent = null;
              Symbol symbol = null;
              Renderer renderer = null;
              DrawingInfo drawingInfo = null;
              object[] fields = null;
              Template template = null;
              EditorTrackingInfo editorTrackingInfo = null;
              AdminLayerInfoAttribute adminLayerInfo = null;
              DefinitionLayer layer = null;

              FeatureLayerAttributes featLayerAttributes = new FeatureLayerAttributes();

              this.Cursor = Cursors.WaitCursor;

              if (featLayerAttributes.extent != null)
            extent = featLayerAttributes.extent;
              else
              {
            extent = new Extent()
            {
              xmin = Convert.ToDouble(txtXMin.Text),
              ymin = Convert.ToDouble(txtYMin.Text),
              xmax = Convert.ToDouble(txtXMax.Text),
              ymax = Convert.ToDouble(txtYMax.Text),
              spatialReference = new SpatialReference() { wkid = Convert.ToInt32(txtSpatialRef.Text) },
            };
              }

            symbol = new Symbol()
            {
              type = "esriPMS",
              url = "RedSphere.png",
              imageData = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQBQYWludC5ORVQgdjMuNS4xTuc4+QAAB3VJREFUeF7tmPlTlE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cMrUnmaFlLX3ysM0fj+t/b5lQLtK22QEfyAmiSLKFZpUJ7kBRPXKW4HqCYynWVHKSG2LkyZex1uO1mZM9lKem9Tx9jjY5iNEYo0bKMhn7ZAu0r6H5PpLXCAq0rKJClSjSGynE/QIkrQYqBPe6S2X+AJsY2Ped6iWZk6RlL0c2r5szofRsO9R5S1IfQLRCpQL1aifoYFerpsbkuTImaUJXuXIDiH6/Ys8vm3Mg8L2i20YqsO7fItKLcSXyn0kXccclVqv3MS6at9JU/Ox+ouns+SF6Z4cSupz7l8+z1ucs7LF1AQjOdxfGZzmx8Iu1TRcfnrioICAQEAgIBgYBAQCAgEBAICAQEAgIBgYBAQCAgEBAICAQEAv8H44b/6ZiGvGAAAAAASUVORK5CYII=",
              contentType = "image/png",
              color = null,
              width = 15,
              height = 15,
              angle = 0,
              xoffset = 0,
              yoffset = 0
            };

            renderer = new Renderer()
            {
              type = "simple",
              symbol = symbol,
              label = "",
              description = ""
            };

            drawingInfo = new DrawingInfo()
            {
              renderer = renderer,
              labelingInfo = null
            };

            List<object> fieldList = new List<object>();
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
              DataGridViewCell cell = dataGridView1.Rows[i].Cells[1];
              if (cell.Value.ToString() == "Double")
              {
            FieldDouble field2 = new FieldDouble()
            {
              name = dataGridView1.Rows[i].Cells[0].Value.ToString(),
              type = "esriFieldTypeDouble",
              alias = dataGridView1.Rows[i].Cells[0].Value.ToString(),
              sqlType = "sqlTypeFloat",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };
              }
              else if (cell.Value.ToString() == "string")
              {
            FieldString field3 = new FieldString()
            {
              name = dataGridView1.Rows[i].Cells[0].Value.ToString(),
              type = "esriFieldTypeString",
              alias = dataGridView1.Rows[i].Cells[0].Value.ToString(),
              sqlType = "sqlTypeNVarchar",
              length = 256,
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };
              }
            }

            //object array so that we can contain different types within.
            //Field type double for example does not contain a length parameter. Hence we need different field type
            //representation. Unexpected properties for data types will cause a failure on the server end.

              fields = fieldList.ToArray();

              if (featLayerAttributes.templates != null)
            template = featLayerAttributes.templates[0];
              else
              {
            template = new Template()
            {
              name = "New Feature",
              description = "",
              drawingTool = "esriFeatureEditToolPoint",
              prototype = new Prototype()
              {
            attributes = new Attributes()
              }
            };
              }

              editorTrackingInfo = new EditorTrackingInfo()
              {
            enableEditorTracking = false,
            enableOwnershipAccessControl = false,
            allowOthersToUpdate = true,
            allowOthersToDelete = true
              };

              adminLayerInfo = new AdminLayerInfoAttribute()
              {
            geometryField = new GeometryField()
            {
              name = "Shape",
              srid = Convert.ToInt32(txtSpatialRef.Text)
            }
              };

              layer = new DefinitionLayer()
              {
            currentVersion = featLayerAttributes != null ? featLayerAttributes.currentVersion : 10.11,
            id = 0,
            name = featLayerAttributes != null ? featLayerAttributes.name != null ? featLayerAttributes.name : "BrewsDownload" : "BrewsDownload",
            type = featLayerAttributes != null ? featLayerAttributes.type != null ? featLayerAttributes.type : "Feature Layer" : "Feature Layer",
            displayField = featLayerAttributes != null ? featLayerAttributes.displayField != null ? featLayerAttributes.displayField : "" : "",
            description = "",
            copyrightText = featLayerAttributes != null ? featLayerAttributes.copyrightText != null ? featLayerAttributes.copyrightText : "" : "",
            defaultVisibility = featLayerAttributes != null ? featLayerAttributes.defaultVisibility != true ? featLayerAttributes.defaultVisibility : true : true,
            relationships = featLayerAttributes != null ? featLayerAttributes.relationShips != null ? featLayerAttributes.relationShips : new object[] { } : new object[] { },
            isDataVersioned = featLayerAttributes != null ? featLayerAttributes.isDataVersioned : false,
            supportsRollbackOnFailureParameter = true,
            supportsAdvancedQueries = true,
            geometryType = featLayerAttributes != null ? featLayerAttributes.geometryType != null ? featLayerAttributes.geometryType : "esriGeometryPoint" : "esriGeometryPoint",
            minScale = featLayerAttributes != null ? featLayerAttributes.minScale : 0,
            maxScale = featLayerAttributes != null ? featLayerAttributes.maxScale : 0,
            extent = extent,
            drawingInfo = drawingInfo,
            allowGeometryUpdates = featLayerAttributes != null ? featLayerAttributes.allowGeometryUpdates != true ? featLayerAttributes.allowGeometryUpdates : true : true,
            hasAttachments = featLayerAttributes != null ? featLayerAttributes.hasAttachments : false,
            htmlPopupType = featLayerAttributes != null ? featLayerAttributes.htmlPopupType != null ? featLayerAttributes.htmlPopupType : "esriServerHTMLPopupTypeNone" : "esriServerHTMLPopupTypeNone",
            hasM = featLayerAttributes != null ? featLayerAttributes.hasM : false,
            hasZ = featLayerAttributes != null ? featLayerAttributes.hasZ : false,
            objectIdField = featLayerAttributes != null ? featLayerAttributes.objectIdField != null ? featLayerAttributes.objectIdField : "FID" : "FID",
            globalIdField = featLayerAttributes != null ? featLayerAttributes.globalIdField != null ? featLayerAttributes.globalIdField : "" : "",
            typeIdField = featLayerAttributes != null ? featLayerAttributes.typeIdField != null ? featLayerAttributes.typeIdField : "" : "",
            fields = fields,
            types = featLayerAttributes != null ? featLayerAttributes.types != null ? featLayerAttributes.types : new object[0] : new object[0],
            templates = new Template[1] { template },
            supportedQueryFormats = featLayerAttributes != null ? featLayerAttributes.supportedQueryFormats != null ? featLayerAttributes.supportedQueryFormats : "JSON" : "JSON",
            hasStaticData = featLayerAttributes != null ? featLayerAttributes.hasStaticData : false,
            maxRecordCount = featLayerAttributes != null ? featLayerAttributes.maxRecordCount != 0 ? featLayerAttributes.maxRecordCount : 2000 : 2000,
            capabilities = featLayerAttributes != null ? featLayerAttributes.capabilities != null ? featLayerAttributes.capabilities : "Query,Editing,Create,Update,Delete" : "Query,Editing,Create,Update,Delete",
            editorTrackingInfo = editorTrackingInfo,
            adminLayerInfo = adminLayerInfo
              };

              DefinitionLayer[] layers = new DefinitionLayer[1] { layer };

              AddDefinition definition = new AddDefinition()
              {
            layers = layers
              };
              string serviceEndPoint = "http://services1.arcgis.com/";
              string serviceEndPoint2 = "http://services.arcgis.com/";
              bool b = RequestAndResponseHandler.AddToFeatureServiceDefinition(serviceEndPoint + _organizationID + "/arcgis/admin/services/" + _featureServiceCreationResponse.Name + ".FeatureServer/AddToDefinition", definition, _token, txtOrg.Text);

              if (!b)
            b = RequestAndResponseHandler.AddToFeatureServiceDefinition(serviceEndPoint2 + _organizationID + "/arcgis/admin/services/" + _featureServiceCreationResponse.Name + ".FeatureServer/AddToDefinition", definition, _token, txtOrg.Text);

              lblSuccess.Text = b == true ? "true" : "false";

              Update();

              this.Cursor = Cursors.Default;
        }