How To Bind Gridview using Json and Webservices
Json Script and for Jquery reference
  1. <script type="text/javascript" src="jquery.min.js"></script>

  2. <script type="text/javascript">
  3. //$(document).ready(function() {
  4. function BindGridView() {
  5. $.ajax({
  6. type: "Post",
  7. url: "WebService.asmx/GetAllRecords",
  8. data: "{}",
  9. contentType: "application/json; charset=utf-8",
  10. dataType: "json",
  11. success: function (data) {
  12. //alert(data.d);
  13. var Employees = data.d;
  14. //var Employees = (typeof data.d) == 'string' ? eval('(' + data.d + ')') : data.d;
  15. $('#grddata').empty();
  16. for (var i = 0; i < Employees.length; i++) {
  17. if (i == 0) {
  18. $('#grddata').append('<table><tr><td><strong>Emp_Title:</strong></td><td>' + Employees[i] + '</td></tr>');
  19. //$('#grddata').append('<p><strong>Emp_Title: ' + Employees[i]+ '</p></strong>');
  20. }
  21. else if (i % 2) {
  22. $('#grddata').append('<tr><td><strong> Emp_Name:</strong> </td><td>' + Employees[i] + '</td></tr>');
  23. }
  24. else {
  25. $('#grddata').append('<table><tr><td><strong>Emp_Title:</strong></td><td>' + Employees[i] + '</td></tr>');
  26. }
  27. }
  28. },
  29. failure: function (data) {
  30. alert("Error Ha..Ha...Ha...");
  31. }
  32. });
  33. }
  34. </script>
Design Page
  1. <body onload="BindGridView();">
  2. <form id="form1" runat="server">
  3. <div id="grddata"> </div>
  4. </form
  5. </body>
Web services code
  1. using System;
  2. using System.Data;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Services;
  7. using System.Web.Services.Protocols;
  8. using System.Xml.Linq;
  9. using System.Data.SqlClient;
  10. using System.Collections.Generic;
  11. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  12. [System.Web.Script.Services.ScriptService]
  13. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  14. //[System.Web.Script.Services.ScriptService]
  15. public class WebService : System.Web.Services.WebService
  16. {
  17. [WebMethod]
  18. public string[] GetAllRecords()
  19. {
  20. SqlConnection con = new SqlConnection("Data Source=BITPLUS5\\SqlExpress;Initial Catalog=WEBHR_22112011;User ID=sa;pwd=bit123;");
  21. con.Open();
  22. //string strQuery = "select Emp_title,Emp_name from tblemployee where emp_name like '" + prefixText + "%'";
  23. string strQuery = "select Emp_title,Emp_name from tblemployee";
  24. DataSet ds = new DataSet();
  25. SqlDataAdapter da = new SqlDataAdapter(strQuery, con);
  26. da.Fill(ds);
  27. con.Close();
  28. List<string> cityList = new List<string>();
  29. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  30. {
  31. cityList.Add(ds.Tables[0].Rows[i]["Emp_title"].ToString());
  32. cityList.Add(ds.Tables[0].Rows[i]["Emp_name"].ToString());
  33. //cityList.Add(ds.Tables[0].Rows[i][0].ToString());
  34. }
  35. con.Close();
  36. return cityList.ToArray();
  37. //return ds;
  38. }
  39. }