Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Administrators
Posted

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)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...