[RESOLVED]Serialize a SQL table then pass it to another webpage to deserialize

I am trying to serialize a SQL table then pass it to another webpage to deserialize:

Default.aspx.cs

            String connStr = ConfigurationManager.ConnectionStrings["db1"].ConnectionString;
            String cmdStr = "SELECT * FROM [Table1];";
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            try
            {
                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
                    {
                        conn.Open();
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        da.Fill(ds);
                        dt = ds.Tables[0];
                        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                        Dictionary<string, object> row;
                        foreach (DataRow dr in dt.Rows)
                        {
                            row = new Dictionary<string, object>();
                            foreach (DataColumn col in dt.Columns)
                            {
                                row.Add(col.ColumnName, dr[col]);
                            }
                            rows.Add(row);
                        }
                        var obj = serializer.Serialize(rows);
                        Hidden1.Value = obj;
                        conn.Close();
                        cmd.Dispose();
                        conn.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                Label2.Text = ex.ToString();
            }

Default.aspx

    <form action="webpage.aspx" method="post">
        <input id="Hidden1" type="hidden" runat="server" />
    </form>

webpage.aspx is missing where you take Hidden1.Value from the default and incorporate into this page:

    <script type="text/javascript">
        $.ajax(
             {
                 url: "~/Default.aspx", 
                 contentType: 'application/json',
                 dataType: "json",
                 type: "POST"
             }).done(function (result) { });

    </script>

webpage.aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            System.Web.Script.Serialization.JavaScriptSerializer JsonConvert = new System.Web.Script.Serialization.JavaScriptSerializer();
            System.Collections.Generic.Dictionary<string, object> items = JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, object>>("datafromjQuery");
            Label1.Text = items.Count.ToString();
         }

Error Code:

The non-generic method 'System.Web.Script.Serialization.JavaScriptSerializer.DeserializeObject(string)' cannot be used with type arguments

Hi Philosophaie,

Thanks for your post.

Philosophaie

protected void Page_Load(object sender, EventArgs e)
        {
            System.Web.Script.Serialization.JavaScriptSerializer JsonConvert = new System.Web.Script.Serialization.JavaScriptSerializer();
            System.Collections.Generic.Dictionary<string, object> items = JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, object>>("datafromjQuery");
            Label1.Text = items.Count.ToString();
         }

According to your description and code, if you’d like to display the total of items in webpage, you could refer to the following sample.

Default.aspx.cs

[WebMethod]
        public static string SelectMethod()
        {
            SqlConnection con = new SqlConnection(yourconnection);
            {
                SqlCommand cmd = new SqlCommand("select * from TestTable", con);
                {
                    SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    sqlDa.Fill(dt);


                    JavaScriptSerializer serializer = new JavaScriptSerializer();

                    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                    Dictionary<string, object> row;
                    foreach (DataRow dr in dt.Rows)
                    {
                        row = new Dictionary<string, object>();
                        foreach (DataColumn col in dt.Columns)
                        {
                            row.Add(col.ColumnName, dr[col]);
                        }
                        rows.Add(row);
                    }

                    string test = serializer.Serialize(rows);  // Serialization
                    return test;

                }

            }
        } 

Webpage.aspx

<script src="../../Scripts/jquery-1.8.2.js"></script>
    <script>
        $(function () {
            $.ajax({ 
                type: "Post", 
                url: "Default.aspx/SelectMethod",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var parsed = $.parseJSON(data.d);
                    var items = 0;
                    $.each(parsed, function (i, jsondata) {
                        //$("#divcontent").append("Id: " + jsondata.Id + "<br/>" + "Values: " + jsondata.Values + "<br/>" + "Name: " + jsondata.Name + "<br/>");
                        items++;
                    });
                    $("#Label1").text(items);
                },
                error: function (err) {
                    alert(err);
                }
            });
        })
    </script> 

    <form id="form1" runat="server">
    <div id="divcontent">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
</form>

Hope it will be helpful to you.

Best Regards,

Fei Han

Leave a Reply