Search code examples
asp.netjsonweb-servicesdatatablewebmethod

Getting the dataset in Json instead of Xml ASP.NET Web service


I am still new to asp.net, and I am currently trying to create a web-service that will get the data from my SQL database in order to display it in an android application.

I am able now to get the data I want from the SQL procedure (that gets customer information) and the response is in XML.

I have searched a lot about a good tutorial showing how to get the response in JSON instead of XML with no luck (also tried some answers I found here on stack overflow) but very less were about dataset.

here is the code I have so far, any help will be appreciated .

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

namespace ART
{    
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class WebService1 : System.Web.Services.WebService
    {
        SqlCommand cmdselect = null, cmdinserted;
        SqlConnection con4, converiler, con3, con;
        SqlDataAdapter da4, da, da3;
        SqlDataReader dr;    

        [WebMethod]         
        public DataSet AboneKullanici(int pkAbone)
        {    
            SqlConnection conFriends = new SqlConnection("Data Source=.;Initial Catalog=TTL;Integrated Security=True;");
            DataSet dsvrs = new DataSet();
            cmdselect = new SqlCommand();
            cmdselect.CommandText = "spAboneKullanici";
            cmdselect.CommandTimeout = 0;
            cmdselect.CommandType = CommandType.StoredProcedure;
            cmdselect.Connection = conFriends;
            da = new SqlDataAdapter(cmdselect);
            cmdselect.Parameters.Add("@aboneid", SqlDbType.Int, 10).Value = pkAbone;
            conFriends.Open();
            da.Fill(dsvrs, "kullaniclar");
            conFriends.Close();
            return dsvrs;
        }
    }
}

Solution

  • You can do it this way

    • Set function descriptors to return JSON
    • Query your data into a DataTable
    • Convert your DataTable to Dictionary
    • Serialize the Dictionary to JSON

    That's it

    [WebMethod()]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string AboneKullanici(int pkAbone) {
        SqlConnection conFriends = new SqlConnection("Data Source=.;Initial Catalog=TTL;Integrated Security=True;");
        DataTable dsvrs = new DataTable("kullaniclar");
        cmdselect = new SqlCommand();
        cmdselect.CommandText = "spAboneKullanici";
        cmdselect.CommandTimeout = 0;
        cmdselect.CommandType = CommandType.StoredProcedure;
        cmdselect.Connection = conFriends;
        da = new SqlDataAdapter(cmdselect);
        cmdselect.Parameters.Add("@aboneid", SqlDbType.Int, 10).Value = pkAbone;
        conFriends.Open();
        da.Fill(dsvrs);
        conFriends.Close();
    
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
        Dictionary<string, object> row;
        foreach (DataRow dr in dsvrs.Rows) {
            row = new Dictionary<string, object>();
            foreach (DataColumn col in dsvrs.Columns) {
                row.Add(col.ColumnName, dr[col]);
            }
            rows.Add(row);
        }
        return serializer.Serialize(rows);
    }