last row in dataset

diane

New member
Joined
Sep 27, 2009
Messages
2
Programming Experience
Beginner
I would like to know how to access the last row in a dataset using the
following
logic. (*Note: the line of code that says 'do something is where I want to
access the last row in the dataset. )
**Note I am using visual basic.net 2005 professional version at work
dim da As New SqlDataAdapter
Dim ds As New DataSet()
da = New SqlDataAdapter(sqlCmd)
sqlCmd.Connection = New SqlConnection(connectionString)
sqlCnn.Open()
da = New SqlDataAdapter(sqlCmd)
sqlCmd.CommandText = sqlCmd.CommandType = CommandType.StoredProcedure
da.Fill(ds)

strholdnumber = 'gets a value
Dim IntTotalRows As Integer = ds.Tables(0).Rows.Count
Dim intFileRows As Integer = UBound(aryFi)
For A = 0 To ds.Tables(0).Rows.Count - 1
For intCntr = 0 To UBound(aryFi) Step 1
If Trim(strholdnumber) = Case(Mid(aryFi(intCntr).Name, 12,5 ))
'do something
End If

Next intCntr
next

the line of code that says: For A = 0 To ds.Tables(0).Rows.Count - 1
,keeps me from accessing the last line.

Thanks!
 
DataSets don't contain rows. DataSets contain DataTables and DataTables contain DataRows. If you want to access just the last row in the DataTable then that's exactly what you should do. You don't need to loop through all rows just to get the last:
VB.NET:
Dim rows As DataRowCollection = ds.Tables(0).Rows
Dim lastRow As DataRow = rows(rows.Count - 1)
You simply get the index of the last row and then get the row at that index.
 
Back
Top