title: How To Return Data type JSON in ashx
body: <p>I'm looking for a way to export the data into a JSON style. Please show me the way</p>
  
  <pre><code>&lt;%@ WebHandler Language="C#" Class="API" %&gt;
  
  using System;
  using System.Web;
  using System.Data;
  using System.Data.SqlClient;
  using System.IO;
  using Newtonsoft.Json;
  using System.Collections.Generic;
  using Newtonsoft.Json.Linq;
  using System.Web.Script.Serialization;
  public class User
  {
      public string type { get; set; }
      public string user { get; set; }
      public string pass { get; set; }
  
  }
  public class API : IHttpHandler
  {
  
  public void ProcessRequest(HttpContext context)
  {
      context.Response.ContentType = "text/plain";
      string strJson = new StreamReader(context.Request.InputStream).ReadToEnd();
      User user = JsonConvert.DeserializeObject&lt;User&gt;(strJson);
      string str = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString, json = "";
      if (user.type != null &amp;&amp; user.user != null &amp;&amp; user.pass != null)
      {
          SqlConnection con = new SqlConnection(str);
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = con;
          cmd.CommandText = "SELECT ID,UserName From Partner Where UserName=@UserName And PassWord=@Pass";
          cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 100).Value = user.user;
          cmd.Parameters.Add("@Pass", SqlDbType.NVarChar, 100).Value = user.pass;
          if (ConnectionState.Closed == con.State)
              con.Open();
          DataTable datatable = new DataTable();
          SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
          con.Close();
          sqlDataAdapter.Fill(datatable);
          if (datatable.Rows.Count &gt; 0)
          {
              foreach (DataRow dr in datatable.Rows)
                  //My data return;
          }
      }
  }
  </code></pre>
  
  <p>Finally I want return data json<br/><img src="https://i.stack.imgur.com/mcWk8.png" alt="My Code HTML/Javascript"></p>
  
  <p>Please help me resolve problem! Thank you</p>