Kris Kris - 2 months ago 11x
SQL Question How to move to the last record

I have the following code. Please help me on how to get the last record of the records.

Dim DteQry As New OdbcCommand
DteQry.CommandText = "select distinct trn_date from order_data order by trn_date asc limit 30;"
DteQry.Connection = Conn

Dim qryRdr As OdbcDataReader
qryRdr = DteQry.ExecuteReader()

If qryRdr.HasRows() Then

Dim StartDte = qryRdr.GetValue(0)
TextBox2.Text = qryRdr.GetValue(0)
Exit Sub
End If


How about this...

You cannot get the row count directly from OdbcDataReader. So just create the dataTable dt and load the dataset from reader to dt. Get the row count from datatable and then you can get last record as below.

If qryRdr.HasRows() Then                
            Dim dt As DataTable
            Dim StartDte = dt.Rows(dt.Rows.Count-1).item(0)
            TextBox2.Text = dt.Rows(dt.Rows.Count-1).item(0)
            Exit Sub
     End If