Solved

vb.net get insert id

Posted on 2006-07-20
7
353 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now