Hi all,
i am new in json script.
am trying to convert the database table value into json using asp.net with c#.
here, am also send the response to asp.net client side.
but am not receiving any response from server side.
So please let me know where i made a mistake.
please find my code below,
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
 
<!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">
    function testJson() {
    $.ajax({
        type: "POST",
        url: "Default.aspx",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var data = eval("(" + msg + ")");
            var t = "<table border=1> <tr>" +
              "<td> <strong>Name</strong></td> <td> " +
              "<strong>Company</strong></td> <td> " +
              "<strong>Address</strong></td> <td> " +
              "<strong>Phone</strong></td> <td> " +
              "<strong>Country</strong></td> </tr> ";
//            jQuery.each(data, function(rec) {
//                t = t + " <tr> <td> " + this.Name + "</td> <td> " +
//                    this.Company + "</td> <td> " + this.Address +
//                     "</td> <td> " + this.Phone + 
//                     "</td> <td> " + this.Country + 
//                     "</td> </tr> ";
//            });
            t = t + " </table> ";
 
            $("#jsonDiv").html(t);
        },
        error: function(msg) {
 
        }
    });
};
    
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div id="jsonDiv">
    
    </div>
    </div>
    </form>
</body>
</html>
 
Code behind:
 
 
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Newtonsoft.Json;
using System.Text;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
     string result=DataTableToJsonWithStringBuilder(getData());
     Response.Write(result);
 
    }
    public DataTable getData()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(@"Data Source=abc\SQL2012;Initial Catalog=test;User ID=sa;Password=test@123");
        con.Open();
        SqlCommand cmd = new SqlCommand("select menuid,caption,ParentID,HasChild,isactive from menu", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
      
        DataSet ds = new DataSet();
      
        da.Fill(dt);
 
        con.Close();
 
        return dt;
        
    }
 
    public string DataTableToJsonWithStringBuilder(DataTable table)
    {
        var jsonString = new StringBuilder();
        if (table.Rows.Count > 0)
        {
            jsonString.Append("[");
            for (int i = 0; i < table.Rows.Count; i++)
            {
                jsonString.Append("{");
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    if (j < table.Columns.Count - 1)
                    {
                        jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
                     + "\":" + "\""
                     + table.Rows[i][j].ToString() + "\",");
                    }
                    else if (j == table.Columns.Count - 1)
                    {
                        jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
                     + "\":" + "\""
                     + table.Rows[i][j].ToString() + "\"");
                    }
                }
                if (i == table.Rows.Count - 1)
                {
                    jsonString.Append("}");
                }
                else
                {
                    jsonString.Append("},");
                }
            }
            jsonString.Append("]");
        }
        
        return jsonString.ToString();
        
    }
}
 
thanks in advance..