andycharger Posted November 18, 2003 Posted November 18, 2003 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 Quote
Administrators PlausiblyDamp Posted November 18, 2003 Administrators Posted November 18, 2003 rough and ready but will show you how - works with northwind but should be easy to adapt to any database. 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) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.