Solved

vb.net get insert id

Posted on 2006-07-20
7
358 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

627 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