Sora
Sora

Reputation: 2551

how can i read a DataTable from javascript

i have a function that return a DataTable like this :

public DataTable SendOnlineContacts()
{
 ...
    for (int i = 0; i < FriendsDt.Rows.Count; i++)
        {
            int FriendID = Convert.ToInt16(FriendsDt.Rows[i][0]);
                DataRow[] FriendisOnlineRow = ConnectedClientDt.Select("ClientID=" + FriendID);

                if (FriendisOnlineRow.Length > 0)  // friend is online 
                {
                  //  new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).Update("Update clients set USER_STATUS='O' where CLIENT_ID=" + FriendsDt.Rows[i][0]);
                    FriendsInfo.Rows.Add(FriendsDt.Rows[i][0] + "," + FriendsDt.Rows[i][1] + "," + FriendsDt.Rows[i][2] + "," + "O");
                  }
           }
            return FriendsInfo;
     }

client side :

$.ajax({
    type: 'POST',
    url: 'ChatPageTest.aspx/SendOnlineContacts',
    data: '{}',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (data) {
     // what to do here to read the DataTable ??
     }
      ...

Please help and thank u

Upvotes: 0

Views: 1396

Answers (2)

Alex Filipovici
Alex Filipovici

Reputation: 32551

Try this:

public object[][] SendOnlineContacts()
{
    //...
    for (int i = 0; i < FriendsDt.Rows.Count; i++)
    {
        int FriendID = Convert.ToInt16(FriendsDt.Rows[i][0]);
        DataRow[] FriendisOnlineRow = ConnectedClientDt.Select("ClientID=" + FriendID);

        if (FriendisOnlineRow.Length > 0)  // friend is online 
        {
            //  new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).Update("Update clients set USER_STATUS='O' where CLIENT_ID=" + FriendsDt.Rows[i][0]);
            FriendsInfo.Rows.Add(FriendsDt.Rows[i][0] + "," + FriendsDt.Rows[i][1] + "," + FriendsDt.Rows[i][2] + "," + "O");
        }
    }

    var rows = FriendsInfo.Rows
        .OfType<DataRow>()
        .Select(row => row.ItemArray)
        .ToArray();
    return rows;
}

Upvotes: 1

Tigran
Tigran

Reputation: 62246

You have to define a format you are able to read in JavaScript, so first compile DataTable to that format after send it to a client. Most common choice in this case is a JSON.

Please have a look on: Convert ASP.NET DataTable to JSON, to use a DataTable in JavaScript for complete implementation details.

Upvotes: 1

Related Questions