Monday, 30 May 2016

asmx return json example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web.Script.Serialization;  

namespace MYTEST
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    [ScriptService]
    public class MYTESTDATA : System.Web.Services.WebService
    {

        SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
        SqlCommand cmd;    
        SqlDataAdapter adp;
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void GetLevel()
        {
            DataTable ds = new DataTable("Kamal");
            con.Open();
            cmd = new SqlCommand("Proc_APP_GETLEVEL", con);
            cmd.CommandType = CommandType.StoredProcedure;
            adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);    
            this.Context.Response.ContentType = "application/json; charset=utf-8";
            this.Context.Response.Write(CreateJsonParameters(ds));
        }
        [WebMethod]
      [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void GetLevelWiseData(int LevelId)
        {
            DataTable ds = new DataTable("Kamal");
            con.Open();
            cmd = new SqlCommand("Proc_APP_GETLEVELWISEDATA", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@LevelId", SqlDbType.DateTime).Value = LevelId;//State-code
            adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            this.Context.Response.ContentType = "application/json; charset=utf-8";
            this.Context.Response.Write(CreateJsonParameters(ds));


        }
        private string CreateJsonParameters(DataTable dt)
        {
     
            StringBuilder JsonString = new StringBuilder();
            //Exception Handling
            if (dt != null)//&& dt.Rows.Count > 0
            {
                JsonString.Append("{ ");
                JsonString.Append("\"Result\":[ ");

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() +
                                  "\":" + "\"" +
                                  dt.Rows[i][j].ToString() + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" +
                               dt.Columns[j].ColumnName.ToString() + "\":" +
                               "\"" + dt.Rows[i][j].ToString() + "\"");
                        }
                    }
                                    
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }

                JsonString.Append("]}");
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }
    
  
      
    }
}

No comments:

Post a Comment

Table Partitioning in SQL Server

  Table Partitioning in SQL Server – Step by Step Partitioning in SQL Server task is divided into four steps: Create a File Group Add Files ...