Solved

vb.net get insert id

Posted on 2006-07-20
7
357 Views
Last Modified: 2008-01-09
I am using an OleDbConnection.
I would like to get back the auto incremented id from the insert command.

        executeSQL("insert into tests (description, offset) values('" & testName & "'," & offsetValue & ")")

    Public Function executeSQL(ByVal sqlstring$)
        Dim myCommand As New SqlCommand(sqlstring)
        Dim retValue%

        Dim myConnection As New OleDb.OleDbConnection(connectionString)

        myConnection.Open()

        Dim dataCommand As New OleDb.OleDbCommand(sqlstring, myConnection)

        retValue = dataCommand.ExecuteNonQuery()

        myConnection.Close()

    End Function

0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17147515
Hi jackjohnson44,

what database are you using, if its sql server you can use @@Identity

if its access or mysql you might need to run a select to bring back the max(id) from the table

Apresto
0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17147705
Yes, you can use @@IDENTITY if you're INSERT will never fire any triggers.  It's actually safer to use SCOPE_IDENTITY(), because it will return the new PKID for that INSERT only.

You'll also probably have to use ExecuteScalar, instead of NonQuery:

        retValue = dataCommand.ExecuteScalar()

Aaaand finally, to bring it all together, the SQL statement would read something like this:

"insert into tests (description, offset) values('" & testName & "'," & offsetValue & ") SELECT SCOPE_IDENTITY()"
0
 
LVL 23

Expert Comment

by:apresto
ID: 17147729
only use a ; between the 2 statements if its an ansi language

"insert into tests (description, offset) values('" & testName & "'," & offsetValue & "); SELECT SCOPE_IDENTITY()"
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:jackjohnson44
ID: 17147833
I am using access
0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17147918
You should still be ok using @@IDENTITY in Access.  See this article for an example:  http://support.microsoft.com/default.aspx/kb/232144
0
 

Author Comment

by:jackjohnson44
ID: 17147969
how do I use @@IDENTITY?

can you please post some code?
0
 
LVL 14

Accepted Solution

by:
jake072 earned 500 total points
ID: 17148113

[After your update code]

Dim cmd As New OleDbCommand("SELECT @@IDENTITY", [YourConnection])
Dim lngID As Long = cmd.ExecuteScalar()

[Close your connection]

Jake
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question