Select a few rows in a DataSet

D

Diego F.

Hi, I'm trying to select the last 3 items from a database to show them. I'm
writing a method that returns the rows in a dataSet. The method is:
public DataSet Last3()

I added a new data field to my database, so I can order the rows and the
first 3 are the newest.

SELECT * FROM Table ORDER BY Date DESC

I fill a DataSet with all the rows, but I don't know how to select just the
first 3 and return them in the dataset.

Regards,

Diego F.
 
C

Cor

Hi Diego,

I do not what you want to do with it,

Using VB it can be.

dim dr1 as datarow =dataset.tables(0).rows(0)
dim dr2 as datarow =dataset.tables(0).rows(1)
dim dr3 as datarow =dataset.tables(0).rows(2)

Than you have the first 3 datarows

I hope this helps?

Cor
 
D

Diego F.

Thanks, I'm trying with that. How can I add the DataRows to a new DataSet?
I'm getting errors, something like "the datarow already belongs to other
datatable".

Regards,

Diego F.
 
M

Miha Markic [MVP C#]

Hi Diego,

Use DataTable.NewRow() method to obtain new row instance.
After you populate it, use DataTable.Rows.Add(yourNewRow)
 
C

Cor

Hi Diego,

Another approach watch typos just rougly typed.
\\\
dim dsnew as new dataset
dsnew = dsold.clone
for i as integer = 0 to 2
dim dr as datarow = dsold.tables(0).newrow
for y as integer = 0 to dsold.tables(0).columns.count - 1
dr(y) = dsold.tables(0).rows(i)y
next
dsnew.tables(0).rows.add(dr)
next
///

I hope this helps,

Cor
 
D

Diego F.

I solved it with the Fill method, using da.Fill(ds,0,3,"Table") instead of
da.Fill(ds), so I get the first three records.

--

Regards,

Diego F.

Miha Markic said:
Hi Diego,

Use DataTable.NewRow() method to obtain new row instance.
After you populate it, use DataTable.Rows.Add(yourNewRow)

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Diego F. said:
Thanks, I'm trying with that. How can I add the DataRows to a new DataSet?
I'm getting errors, something like "the datarow already belongs to other
datatable".

Regards,

Diego F.
 
C

Cor

Hi Diego,

Greath, that is of course better, I never thought to look at that, there are
so much overloaded functions from the Fill (the page where this is on is as
far as I can see even not complete).

But now I do not forget it also.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top