title: ASHX To Serialize SQL Data To GeoJSON Format
body: <p>I am trying to build a web application that pulls data from SQL Server through an ASHX Handler, convert to a valid GeoJSON format, and display the markers on a leaflet map. I have the query to pull three fields from the SQLServer (Description, LAT, LONG). I have the ability to display GeoJSON data in Leaflet (from website documentation). What I can't figure out is how to successfully build GeoJSON data. Is there an easy way to build a GeoJSON Object using the Javascript Serializer, or is it a process I have to build in the hanlder.</p>
  
  <p>Here is an example GeoJSON file I would like to create:</p>
  
  <pre><code>{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
          "name": "Placemarker 1",
          "marker-color": "#0000ff",
          "marker-symbol": "airport"
        },
        "geometry": {
          "type": "Point",
          "coordinates": [
            -77.12911152370515,
            38.79930767201779
          ]
        }
      },
      {
        "type": "Feature",
        "properties": {
          "name": "Placemarker 2",
          "marker-color": "#FF0000",
          "marker-symbol": "hospital"
        },
        "geometry": {
          "type": "Point",
          "coordinates": [
            -7.12911152370515,
            5.79930767201779
          ]
        }
      }]
  }
  </code></pre>
  
  <p>Here is contents of an ASHX file that I have that currently build a simple JSON File:</p>
  
  <pre><code>private class DataSet
  {
      public string description { get; set; }
      public double valueLat { get; set; }
      public double valueLong { get; set; }
  }
  
  public void ProcessRequest(HttpContext context)
  {
      List&lt;DataSet&gt; listResults = new List&lt;DataSet&gt;();
      int recordCount = 0;
  
      try
      {
          // Get Connection String From WEB.CONFIG
          string connStr = ConfigurationManager.ConnectionStrings["myConnStr"].ConnectionString;
  
          // Connect And Get Data
          OdbcConnection sqlConn = new OdbcConnection(connStr.ToString());
          OdbcCommand sqlCmd = new OdbcCommand("{call getSampleData}", sqlConn);
          sqlConn.Open();
          OdbcDataReader rdr = sqlCmd.ExecuteReader();
  
          while (rdr.Read())
          {
              DataSet results = new DataSet();
              results.description = rdr["description"].ToString();
              results.valueLat = Convert.ToDouble(rdr["lat"]);
              results.valueLong = Convert.ToDouble(rdr["long"]);
              listResults.Add(results);
              recordCount++;
          }
  
          sqlConn.Close();
      }
      catch (OdbcException o)
      {
         context.Response.Write(o.Message.ToString());
      }
  
      var result = new
      {
          iTotalRecords = recordCount,
          aaData = listResults
      };
  
      JavaScriptSerializer js = new JavaScriptSerializer();
      context.Response.Write(js.Serialize(result));
  }
  </code></pre>