How to bind Gridview Using Json and Webservices

By Dhaval Patel Nov 30, 2011
Json using Bind Div,Webservice- json using Bind div,Webservices using bind div,json call webservices and bind div
How To Bind Gridview using Json and Webservices

Json Script and for Jquery reference

  <script type="text/javascript" src="jquery.min.js"></script>

<script type="text/javascript">
//$(document).ready(function() {
function BindGridView() {
type: "Post",
url: "WebService.asmx/GetAllRecords",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data)
var Employees = data.d;
//var Employees = (typeof data.d) == 'string' ? eval('(' + data.d + ')') : data.d;
for (var i = 0; i < Employees.length; i++)
if (i==0)
$('#grddata').append('<table><tr><td><strong>Emp_Title:</strong></td><td>' + Employees[i] + '</td></tr>');
//$('#grddata').append('<p><strong>Emp_Title: ' + Employees[i]+ '</p></strong>');
else if(i%2)
$('#grddata').append('<tr><td><strong> Emp_Name:</strong> </td><td>' + Employees[i]+ '</td></tr>');
$('#grddata').append('<table><tr><td><strong>Emp_Title:</strong></td><td>' + Employees[i] + '</td></tr>');

alert("Error Ha..Ha...Ha...");

Design page
 <body onload="BindGridView();">
<form id="form1" runat="server">
<div id="grddata">

Web services code
using System;
using System.Data;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

[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[] GetAllRecords()
SqlConnection con = new SqlConnection("Data Source=BITPLUS5\\SqlExpress;Initial Catalog=WEBHR_22112011;User ID=sa;pwd=bit123;");

//string strQuery = "select Emp_title,Emp_name from tblemployee where emp_name like '" + prefixText + "%'";
string strQuery = "select Emp_title,Emp_name from tblemployee";

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strQuery, con);
List<string> cityList = new List<string>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

return cityList.ToArray();
//return ds;

Dhaval Patel
Dhaval Patel

Hi, AllDhaval patel is working as software developer and total 3.0 Years experience.