0

I have Stored my Datatable in a Session variable as

Session["dt"]=dt;

and I am accessing it in the Javascript for some purpose as

var oTable = '<%=Session["dt"] %>';
var oRows = oTable.fnGetNodes(); // Error here     
for (var i = 0; i < oRows.length; i++) {

var x = parseInt(cells.push($(oRows[i]).find("td:eq(1)").html()));
}

but it throws the error Object does not Support this property what could be going wrong kindly any one help

Edit : For Your Information I am just getting the datatable to fetch the Values in it and use it for some condition

I had found via your answers without serilization it may not be possible so I had serialized the data and passed the value to hiddenfield I had done as follows

 myArray[0] = new { Flag = dt.Rows[0]["Flag"].ToString(), 
                    status = dt1.Rows[0]["Status"].ToString() };
 JavaScriptSerializer serializer1 = new JavaScriptSerializer();

 sbAllUsers = serializer1.Serialize(myArray);
 hfvalue.Value = sbAllUsers;

but I dont know how to access this value in Jquery kindly anyone help me.

4
  • I think you need to convert your datatable to an HTML string and then call the .datatable() function on THAT string. Commented Jun 2, 2014 at 6:51
  • you have to first convert the Session value to the appropriate type because session store object type value. Commented Jun 2, 2014 at 6:53
  • refer : stackoverflow.com/q/10492791/3660930 Commented Jun 2, 2014 at 6:53
  • @rajesh could you pl upvote if its really helps you Commented Jun 3, 2014 at 4:21

2 Answers 2

4

As mentioned below create a list of anynomous class from DataTable and serialize using JavaScriptSerializer. and set the result in hiddenfield hdnControl, now on client side get the value from hidden field and parse it into the JSON, now you can access the data.

C#

JavaScriptSerializer oSerializer = new JavaScriptSerializer();

var Result = (from c in dt.AsEnumerable()
              select new
              {
                  Flag = c.Field<bool>("Flag"),
                  Status = c.Field<string>("Status")
              }).ToList();

hdnControl.Value = oSerializer.Serialize(Result);

Javascript

var oTable = JSON.parse($("#hdnControl").val());
$(oTable).each(function(index, val){
    console.log(val.Flag);
    console.log(val.Status);
})
Sign up to request clarification or add additional context in comments.

Comments

0

Object does not support .... --> is a Javascript error...

You cannot overhand a complex type like this. You have to serialize it first to string format --> JSON string or a normal string.

Perhaps this post might help you, how to serialize a datatable into a json string: Convert datatable to JSON in C#

protected void Page_Load(object sender, EventArgs e)
    {            
        ICollection<MessageDTO> list = new List<MessageDTO>();
        for (int i = 0; i < 10; i++)
        {
            var obj = new MessageDTO() { UserId = i, DateCreated = DateTime.Now, Content = i.ToString(), ChatRoomId = 1, MessageId = i };
            list.Add(obj);
        }

        MyCustomStringProp = JsonConvert.SerializeObject(list);
    }

    public string MyCustomStringProp { get; set; }

Fetching the data from Javascript: --> Download the following JSON script and reference it in ur page: JSON2.js

 var oTable = '<%= this.MyCustomStringProp %>';
            var parsed = JSON.parse(oTable);
            alert(oTable);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.