Click here to Skip to main content
13,048,846 members (78,464 online)
Rate this:
Please Sign up or sign in to vote.
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 using c#.pls help me
Posted 29-Oct-12 2:38am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

try the following code.

DataTable dt1_new = new DataTable();
Rate this: bad
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:[^]

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
SELECT TOP 1 FieldA, FieldB FROM table2
SELECT TOP 1 FieldA, FieldB FROM table3
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2017
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