0

How to show merge tables(number of tables) data in descending order according to date/time column?

According to this code I have a table named 'Friend'.

SqlDataAdapter getnewsfeeds;
SqlDataAdapter friendadapter = new SqlDataAdapter("select * from " + Session["username"].ToString() + "_friends ", friendsconnection);
friendadapter.Fill(friendsdataset);
int friendrowcount = friendsdataset.Tables[0].Rows.Count;
if (friendrowcount > 0)
{
      getnewsfeeds = new SqlDataAdapter("select * from " + Session["username"].ToString() + "_newsfeeds ", con);
      getnewsfeeds.Fill(ds2);
      for (int i = 0; i < friendrowcount; i++)
      {
          friendstringtable = " " + friendsdataset.Tables[0].Rows[i]["friendsusername"].ToString() + "_newsfeeds ";
          getnewsfeeds = new SqlDataAdapter("select * from " + friendstringtable + "", con);
          getnewsfeeds.Fill(ds2);
      }

}
ds2.Tables[0].DefaultView.Sort = "time_date desc";

Data of merge tables are showing but it is not showing according to date/time in descending order. (For eg Facebook Homepage).

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

2

I don't think you can do it in-place, you're going to need to create a new DataTable:

DataView dv = ds2.Tables[0].DefaultView;
dv.Sort = "time_date desc";
DataTable sorted = dv.ToTable();

Here is a good reference.

Having said that, wouldn't it be better/easier/faster to do it in your sql query ?

getnewsfeeds = new SqlDataAdapter(
   "select * from " + friendstringtable + " order by time_date desc", con);

And while this is not your question, my advice is NOT to have a table for each user. I know most people don't come here for "advice", but rather for direct answer, however in this case I'm afraid I have to say something.

Community
  • 1
  • 1
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79