Consume Json Web Service in ASP.Net using C# and VB.Net

Last Reply on Jun 15, 2017 04:45 AM By AnandM

Posted on Jun 14, 2017 03:07 AM



I want to make restful webservice which fetch data from sql server database and give out in json format.


Please share some snippet on internet it is very complex i believe you make it very easy to understand and useful thanks

Posted on Jun 14, 2017 04:35 AM


i want some thing like this but this tutorial is very complex and using store procedure i want sql statement in web service instead of store procedure

Posted on Jun 15, 2017 04:45 AM Modified on on Jun 15, 2017 04:46 AM

Hi nauna,

I have created a sample which full fill your requirement you need to modify the code according to your need.

I have used Northwind Database Customers Table you can get Database from below link.

Install the Northwind and Pubs Sample Databases in SQL Server Express

Refer below Sample Code.


<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>


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

/// <summary>
/// Summary description for WebService
/// </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. 
public class WebService : System.Web.Services.WebService
    public string GetCustomers()
        List<object> customers = new List<object>();
        string sql = "SELECT TOP 10 CustomerId,ContactName,Country FROM Customers";
        using (SqlConnection conn = new SqlConnection())
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand(sql))
                cmd.Connection = conn;
                using (SqlDataReader sdr = cmd.ExecuteReader())
                    while (sdr.Read())
                            CustomerId = sdr["CustomerId"],
                            ContactName = sdr["ContactName"],
                            Country = sdr["Country"]
            return (new JavaScriptSerializer().Serialize(customers));


    <table id="tblCustomers">
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $(function () {
                type: "POST",
                url: "WebService.asmx/GetCustomers",
                data: "",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {                        
                    $.each($.parseJSON(r.d), function (key, value) {
                        var customerId = value.CustomerId;
                        var contactName = value.ContactName;
                        var country = value.Country;
                        $('[id*=tblCustomers]').append("<tr><td>" + customerId + "</td><td>" + contactName + "</td><td>" + country + "</td></tr>");
                error: function (r) {
                failure: function (r) {



CUSTOMERID ContactName Country
AAFKM Mudassar Khan Belgium
ALFKI Maria Austria
ANATR Ana Trujillo France
ANTON Antonio Moreno Brazil
AROUT Thomas Hardy Ireland
BERGS Christina Berglund Italy
BLAUS Hanna Moos Finland
BLONP Frédérique Citeaux Finland
BOLID Martín Sommer Argentina
BONAP Laurence Lebihan USA