# Other Languages > jQuery >  json and jquery gridview problem in binding

## ERUM

i have following code in json/jquery/grid connectivity 

```
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="json.aspx.cs" Inherits="LINQ.json" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {

        //    function () {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Service1.asmx/BindDatatable",
            async: true,
            cache: false,
            data: "{}",
            dataType: "json",
            success: function (data) {
                for (var i = 0; i < data.d.length; i++) {
                    $("#gvDetails").append("<tr><td>" + data.d[i].OfficeName + "</td><td>" + data.d[i].City + "</td><td>" + data.d[i].Country + "</td></tr>");

                }
            },
            error: function (result) {
                alert(result.toString());
            }
        });

        $('#btnSearch').click
                            (
                                function () {

                                    var searchtext = $("#txtSearch").val();

                                    alert(searchtext);
                                    $.ajax(
                                                {
                                                    type: "POST",
                                                    url: "Gridview.aspx/BindSearchDatatable",
                                                    //data: "{officename : '"+searchtext+"'}",
                                                    data: JSON.stringify({ officename: searchtext }),
                                                    contentType: "application/json; charset=utf-8",
                                                    dataType: "json",
                                                    async: true,
                                                    cache: false,
                                                    success: function (data) {


                                                        for (var i = 0; i < data.d.length; i++) {

                                                            $("#gvDetails").append("<tr><td>" + data.d[i].OfficeName + "</td><td>" + data.d[i].City + "</td><td>" + data.d[i].Country + "</td></tr>");
                                                        }
                                                    },
                                                    error: function (x, e) {
                                                        alert("The call to the server side failed. " + x.responseText);
                                                    }
                                                }
                                            );
                                    return false;
                                }
                            );


        // }
    });

</script>
</head>
<body>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td>
                <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:Button ID="btnSearch" runat="server" Text="Search" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:GridView ID="gvDetails" runat="server">
                    <HeaderStyle BackColor="#DC5807" Font-Bold="true" ForeColor="White" />
                </asp:GridView>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
```

and add webserevice and have following class of webserice 

```
  public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public static OfficeDetails[] BindDatatable()
        {
            DataTable dt = new DataTable();
            List<OfficeDetails> details = new List<OfficeDetails>();

            dt.Columns.Add("OfficeName", typeof(string));
            dt.Columns.Add("City", typeof(string));
            dt.Columns.Add("Country", typeof(string));

            dt.Rows.Add("OfficeName", "Emmaculate");
            dt.Rows.Add("City", "khi");
            dt.Rows.Add("Country", "pak");
            foreach (DataRow dtrow in dt.Rows)
            {
                OfficeDetails Office = new OfficeDetails();
                Office.OfficeName = dtrow["OfficeName"].ToString();
                Office.City = dtrow["City"].ToString();
                Office.Country = dtrow["Country"].ToString();
                details.Add(Office);
            }

            //using (SqlConnection con = new SqlConnection(@"Data Source=kartikpatel\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"))
            //{
            //    using (SqlCommand cmd = new SqlCommand("select  OfficeName,City,Country from Office", con))
            //    {
            //        con.Open();
            //        SqlDataAdapter da = new SqlDataAdapter(cmd);
            //        da.Fill(dt);
            //        foreach (DataRow dtrow in dt.Rows)
            //        {
            //            OfficeDetails Office = new OfficeDetails();
            //            Office.OfficeName = dtrow["OfficeName"].ToString();
            //            Office.City = dtrow["City"].ToString();
            //            Office.Country = dtrow["Country"].ToString();
            //            details.Add(Office);
            //        }
            //    }
            //}
            return details.ToArray();

        }
    }
```

and another class 

```
   public class OfficeDetails
    {
        public string OfficeName { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }
```


the only thing i m doing is to bind grid with data return from webserice method

but wheni execute this code ..it gives me an error in alert box as [object object]
can any body help me !!




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

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public static OfficeDetails[] BindDatatable()
        {
            DataTable dt = new DataTable();
            List<OfficeDetails> details = new List<OfficeDetails>();

            dt.Columns.Add("OfficeName", typeof(string));
            dt.Columns.Add("City", typeof(string));
            dt.Columns.Add("Country", typeof(string));

            dt.Rows.Add("OfficeName", "Emmaculate");
            dt.Rows.Add("City", "khi");
            dt.Rows.Add("Country", "pak");
            foreach (DataRow dtrow in dt.Rows)
            {
                OfficeDetails Office = new OfficeDetails();
                Office.OfficeName = dtrow["OfficeName"].ToString();
                Office.City = dtrow["City"].ToString();
                Office.Country = dtrow["Country"].ToString();
                details.Add(Office);
            }

            //using (SqlConnection con = new SqlConnection(@"Data Source=kartikpatel\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"))
            //{
            //    using (SqlCommand cmd = new SqlCommand("select  OfficeName,City,Country from Office", con))
            //    {
            //        con.Open();
            //        SqlDataAdapter da = new SqlDataAdapter(cmd);
            //        da.Fill(dt);
            //        foreach (DataRow dtrow in dt.Rows)
            //        {
            //            OfficeDetails Office = new OfficeDetails();
            //            Office.OfficeName = dtrow["OfficeName"].ToString();
            //            Office.City = dtrow["City"].ToString();
            //            Office.Country = dtrow["Country"].ToString();
            //            details.Add(Office);
            //        }
            //    }
            //}
            return details.ToArray();

        }
    }

----------


## kareninstructor

Hello,

I am not a C Sharp programmer yet unless I am missing something I do not see you serializing the list to json in BindingDataTable then deserializing to OfficeDetails.

----------


## ERUM

any one else can particapate in problem resolution!!!

thanks in advance  :Smilie:

----------


## kareninstructor

First off sorry about using VB.NET my converter did not convert to C properly

In the web service I would look at using JSON.NET

I am a VB.NET developer, if I were to send a record back in our shop we always send a record count and status along with the data i.e.


```
Imports Newtonsoft.Json
Public Class OfficeDetailsDataSet
    Public Property returnStatus As DetailsStructure
    Public Sub New()
        returnStatus = New DetailsStructure
    End Sub
    Public Overrides Function ToString() As String
        Return JsonConvert.SerializeObject(Me)
    End Function
End Class
Public Class DetailsStructure
    Public Property Status As String
    Public Property RecordCount As Int32
    Public RecordSet As OfficeRecords
    Public Sub New()
        Me.Status = "SUCCESS"
        Me.RecordCount = 1
        RecordSet = New OfficeRecords
    End Sub
End Class
Public Class OfficeRecords
    Public Property Record As OfficeDetails()
    Public Sub New()
        '
        ' Simulation: 
        '   Values would come from a DataRow for a real project.
        '   Keeping the database part out allows to concentrate on the json
        Dim Item1 As New OfficeDetails("AAA Office", "Portland", "USA")
        Me.Record = {Item1}
    End Sub
End Class
Public Class OfficeDetails
    Public Property OfficeName As String
    Public Property City As String
    Public Property Country As String
    Public Sub New(ByVal OfficeName As String, ByVal City As String, ByVal Country As String)
        Me.OfficeName = OfficeName
        Me.City = City
        Me.Country = Country
    End Sub
End Class
```

usage


```
Dim MyStruct As New OfficeDetailsDataSet
```

Then we use the following to obtain the json string


```
MyStruct.ToString
```

Then the caller has the following


```
{"returnStatus":{"RecordSet":{"Record":[{"OfficeName":"AAA Office","City":"Portland","Country":"USA"}]},"Status":"SUCCESS","RecordCount":1}}
```

Formatted and validated, validator


```
{
   "returnStatus":{
      "RecordSet":{
         "Record":[
            {
               "OfficeName":"AAA Office",
               "City":"Portland",
               "Country":"USA"
            }
         ]
      },
      "Status":"SUCCESS",
      "RecordCount":1
   }
}
```

----------

