Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
hi,
i have a dataset which has 4 datatables in it and some records in each datatable.i need to select oly the top one record from these 4 tables and save it in either a datatable or any dataset and display these 4 records in my grid. pls help me how to do it in asp.net using c#.pls help me
Posted 29-Oct-12 3:38am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try the following code.
 
  DataTable dt1_new = new DataTable();
  dt1_new.Rows.Add(dt1.AsEnumerable().FirstOrDefault());
  dt1_new.Rows.Add(dt2.AsEnumerable().FirstOrDefault());
  dt1_new.Rows.Add(dt3.AsEnumerable().FirstOrDefault());
  dt1_new.Rows.Add(dt4.AsEnumerable().FirstOrDefault()); 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Getting the first record from a DataTable is not a hard thing to do. Simply access row 0 only. That would look something like this:
var row = DataSet.DataTables[0].Rows[0];
That will give you a DataRow that contains the first record from the first table in the DataSet. You could do this for each of your four tables. Then you could create a new DataTable and insert these four rows into it. The key here though would be that the four rows would have to have matching column names and types. Here is how you would manually create a DataTable:
http://www.dotnetperls.com/datatable[^]
 
The bigger question I see here though is why are you do it this way? It seems like you could at least limit your query to only give you the first row in each table (SELECT TOP 1 * FROM table). You might also be able to do the joining of the four entries at the database level as well. Something like this:
SELECT TOP 1 FieldA, FieldB FROM table1
UNION ALL
SELECT TOP 1 FieldA, FieldB FROM table2
UNION ALL
SELECT TOP 1 FieldA, FieldB FROM table3
UNION ALL
SELECT TOP 1 FieldA, FieldB FROM table4
That would give you the top record from each table and join them together. The output would be four rows.
 
Obviously these last suggestions would be dependent on how your environment is set up, but if possible I would definitely recommend going this way.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100