Thursday 1 March 2012

datatable to json and show html table


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Collections.Generic;
public partial class jquerypagemethod : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // Label1.Text = HiddenField1.Value;
       // Session["name"] = "prnay";
    }

   [System.Web.Script.Services.ScriptMethod(ResponseFormat=ResponseFormat.Json)]
   [ System.Web.Services.WebMethod()]
   //[System.Web.Script.Services.ScriptMethod()]
    //string  cateogryID
    public static string  GetProducts()
  
   {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);
        con.Open();
        SqlCommand com = new SqlCommand("SELECT top 1 *  FROM STOCK ", con);
        com.CommandType = CommandType.Text;
        SqlDataAdapter ada = new SqlDataAdapter(com);
        DataTable objStockDT = new DataTable();
        ada.Fill(objStockDT);
        //StringBuilder newstring = new StringBuilder();
        string my =  GetJSONString(objStockDT);
        return my;
    }


   public static string GetJSONString(DataTable Dt)
   {

       string[] StrDc = new string[Dt.Columns.Count];
       string HeadStr = string.Empty;
       Dt.TableName = "dt";
       for (int i = 0; i < Dt.Columns.Count; i++)
       {

           StrDc[i] = Dt.Columns[i].Caption;

           HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";
       }

       HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);

       StringBuilder Sb = new StringBuilder();
       Sb.Append("{\"" + Dt.TableName + "\" : [");

       for (int i = 0; i < Dt.Rows.Count; i++)
       {

           string TempStr = HeadStr;
         Sb.Append("{");

           for (int j = 0; j < Dt.Columns.Count; j++)
           {

               TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "¾", Dt.Rows[i][j].ToString());
          
           }

        Sb.Append(TempStr + "},");
       }

       Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
      Sb.Append("]}");

       return Sb.ToString();
   }




    <script type="text/javascript">

        function outputDT(dataTable) {
            var headers = [];
            var rows = [];

            headers.push("<tr>");
            for (var name in dataTable[0])
                headers.push("<td><b>" + name + "</b></td>");
            headers.push("</tr>");

            for (var row in dataTable) {
                rows.push("<tr>");
                for (var name in dataTable[row]) {
                    rows.push("<td>");
                    rows.push(dataTable[row][name]);
                    rows.push("</td>");
                }
                rows.push("</tr>");
            }

            var top = "<table border='1'>";
            var bottom = "</table>";

            return top + headers.join("") + rows.join("") + bottom;
        }
    </script>

    <script type="text/javascript">
        $(document).ready(function () {
            $("p").click(function () {
                alert("hi");
                var areaId = 42;
                var jqxhr = $.ajax({
                    type: "POST",
                    //  url: "jquerypagemethod.aspx/GetRegions",
                    url: "jquerypagemethod.aspx/GetProducts",
                    // data: "{cateogryID:" + areaId + "}",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {


                        //  var json3 = { "dt": [{ "SRNO": "1", "REPORT_NO": "1109681094", "CONTROL_NO": "", "PACKET_NO": "D226A1B", "SHAPE": "BR"}] }



                        json3 = JSON.parse(data.d);
                        // alert(json3.dt);
                        $('#My').append(CreateTableView(json3.dt, "lightPro", true)).fadeIn();
                        $(outputDT(json3.dt)).appendTo("#Newtable");

                    }
                });
            });
        });
</script>


No comments:

Post a Comment