Selecting one field from the MAX?

andycharger

Centurion
Joined
Apr 2, 2003
Messages
152
Hi all.
Im trying to get the id of a record I have just added in my database. As it will be the last record im doing:-

"select max from errors"

Trouble is, im not sure how to get the field I want from the record!!!

For example, I can get an entire row back, stick it in a sqldatareader and put it in a datagrid.

But that is not what I want. I dont want to put it in a datagrid, I just want to get the ID of the row and assign it to a variable so I can reuse it.
Can someone show me how I do this?

Cheers.

Andy
 
rough and ready but will show you how - works with northwind but should be easy to adapt to any database.

Visual Basic:
Dim conn As New SqlClient.SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=true")
conn.open()
Dim cmd As New SqlClient.SqlCommand()
cmd.CommandText = "SELECT MAX(EmployeeID) From Employees"
cmd.CommandType = CommandType.Text
cmd.Connection = conn
Dim i As Integer = DirectCast(cmd.ExecuteScalar(), Integer)
 
Back
Top