Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find last row in dataset

Status
Not open for further replies.

jennyek2000

Programmer
Sep 2, 2003
43
GB
Im new to VB.NET
How do you find the last row in a table??

Jenny
 
Jenny

datatable.rows.count gives you the number of rows.

you can do a datatable.select to return a collection of datarows, if you know anything about the last record.
eg. dim foundrows as datarow() = dt.select("primary_key=30")

If you need to navigate your data table, you need to tie it to a currency manager. Changing the currency manager position, then changes the selected row in the datatable.




WTrueman
...if it works dont mess with it
 
Thanks, but still a bit stuck..
I want to retreive the value of the primary key of the last row in a dataset. I have used the following code so far,

Dim mydataset As New DataSet()

myDataAdapter = New OleDbDataAdapter("SELECT * FROM transmittals", myConnection)
myDataAdapter.Fill(mydataset, "transmittals")

Dim bmtransmittals As BindingManagerBase
bmtransmittals = Me.BindingContext(mydataset, "Transmittals")
bmtransmittals.Position = bmtransmittals.Count - 1

Hopefully this code has got me to the last row in the dataset? But now how do I retreive the value of the primary
key (transNo) so that I can display it in a text box?

Thanks,
Jenny
 
Jenny

Something like this should do the trick
Its long winded I know. Its worth spending some time to subclass the Datatable and build in your own custom methods to handle reading and writing to fields in your table

Code:
Dim drv As DataRowView = dt.DefaultView(oCurrencyManager.Position)
Dim drow As DataRow = drv.Row
dim myTransNo as Integer = drow.Item("Transno")



WTrueman
...if it works dont mess with it
 
I can only think of one reason to retreive the last value of an SQL SELECT statment without an ORDER BY clause and that is that your primary key is an IDENTITY (autonumber). If that is the case, why not just ask the database:

SELECT TOP 1 MyPrimaryKey FROM transmittals ORDER BY MyPrimaryKey DESC

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top