Monday, June 20, 2022

How to convert dataset to JSON with webservice ASP.NET C# using Luminious.DataAcessLayer.SqlHelper.ExecuteDataset

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Data;

using System.Collections;

using System.Web.Script.Serialization;

/// <summary>

/// Summary description for MIETY_SkillDevelopment

/// </summary>

[WebService(Namespace = "/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

 [System.Web.Script.Services.ScriptService]

public class MIETY_SkillDevelopment : System.Web.Services.WebService {


    String frmName = "Miety Skill Development";

    public MIETY_SkillDevelopment () {


        //Uncomment the following line if using designed components 

        //InitializeComponent(); 

    }


    [WebMethod]

    public void GetMIETY_SkillDevelopment_Achievement()

    {

        DataSet ds = new DataSet();

        try

        {

            //string Query = "select 'Miety Skill Development Achievement' as 'Name',SD_ESDM + SD_NIELIT + SD_CDAC as 'Total' from [T004_SkillDevelopment]";

            string Query = @"select 

0 as mcode,

0 as state_code,

0 as district_code,

0 as teh_code,

0 as blk_code,

0 as sector_code,

0 as gp_code,

0 as vill_code,

0 as dept_code,

0 as project_code,

SD_ESDM as cnt1,

SD_NIELIT as cnt2,

SD_CDAC as cnt3,

0 as cnt4,

0 as cnt5,

7 as dataportmode,-- for quarterly

case when convert(varchar,SD_Date,101) between '01/01/2019' AND '03/31/2019' THEN '1'

when convert(varchar,SD_Date,101) between '01/04/2019' AND '06/30/2019' THEN '2'

when convert(varchar,SD_Date,101) between '07/01/2019' AND '09/30/2019' THEN '3'

when convert(varchar,SD_Date,101) between '10/01/2019' AND '12/31/2019' THEN '4'

else '0' end as modedesc, --1 quarter1(jan feb mar)  2-- quarter2 (apr may june)

1 as data_lvl_code, --1 for centrally

year(SD_Date) as yr,

month(SD_Date) as mnth,

convert(varchar,CURRENT_TIMESTAMP,101) as Datadt

from [T004_SkillDevelopment]";


            //DataTable ob = null;

            ds = Luminious.DataAcessLayer.SqlHelper.ExecuteDataset(Luminious.Connection.Configuration.ConnectionString, System.Data.CommandType.Text, Query);



            //ds.Tables.Add();


            try

            {

                System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer() { MaxJsonLength = 86753090 };

                ArrayList root = new ArrayList();

                List<Dictionary<string, object>> table;

                Dictionary<string, object> data;


                foreach (DataTable dt in ds.Tables)

                {

                    table = new List<Dictionary<string, object>>();

                    foreach (DataRow dr in dt.Rows)

                    {

                        data = new Dictionary<string, object>();

                        foreach (DataColumn col in dt.Columns)

                        {

                            data.Add(col.ColumnName, dr[col]);

                        }

                        table.Add(data);

                    }

                    root.Add(table);

                }



                JavaScriptSerializer js = new JavaScriptSerializer();

                Context.Response.Write(js.Serialize(root));


            }

            catch { }

            //clsCommon.prcAddLog(frmName + " - Record Sucessfully Processed",

            //                  "Send " + ds.Tables[0].Rows.Count.ToString() + " records to DBT Agriculture",

            //                  "",

            //                  "",

            //                  "");

            //return ds;

            //}

        }

        catch (Exception ex1)

        {

            //clsCommon.prcAddLog(frmName + " - Get PMKSY Beneficiary Count For DBT Agriculture",

            //        "Error",

            //        (ex1.Message == null ? "" : Microsoft.Security.Application.Encoder.HtmlEncode(ex1.Message)),

            //        (ex1.InnerException == null ? "" : Microsoft.Security.Application.Encoder.HtmlEncode(ex1.InnerException.ToString())),

            //        (ex1.StackTrace == null ? "" : Microsoft.Security.Application.Encoder.HtmlEncode(ex1.StackTrace.ToString())));


           // return ds;

        }


    }

    

    

}


No comments:

Post a Comment