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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Value from Last Record in DB 1

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
I'm trying to look at the last record inserted into a SQL table. I'm not getting an errors using the below code but cannot get text to appear in the textbox.

Thanks in advance

Protected Function getLastRec() As DataRow

Dim con As New SqlConnection(strcn)
Dim da As New SqlDataAdapter("select field from Table", con)
Dim ds As New DataSet()
con.Open()
da.Fill(ds)
Dim dr As DataRow = DirectCast(ds.Tables(0).Rows(ds.Tables(0).Rows.Count - 1), DataRow)
Return dr
t1.Text = dr.Item(0).ToString

End Function

 
try something like that

Code:
If ds.Tables,Count > 0 Then
   if ds.Tables(0).Rows.Count > 0 then
    Dim dr As DataRow = ds.Tables(0).Rows(ds.Tables(0).Rows.Count - 1))
    t1.Text = dr.Item(0).ToString
   end if
End If

or preferable change your query to
"select top 1 field from table order by field desc"
 
jbenson001. thanks for that. didn't catch that.

gk53, that's pretty much what I ended up doing.... thanks

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top