John Nuñez
John Nuñez

Reputation: 1830

Return Last ID (IDENTITY) On Insert row VB.NET MySQL

Dim insert_coupon_query As String = ("INSERT INTO qa_discountcoupons (id, status_code) VALUES (AUTO_INCREMENT_ID, 5)")
                Dim cmd_query As New MySqlCommand(insert_coupon_query, objConn)
                Dim cmd_result As Integer = CInt(cmd_query.ExecuteScalar())

I want to return the AUTO_INCREMENT value of the current insert, and show in a msgbox.

Upvotes: 8

Views: 32857

Answers (3)

Ian Halligan
Ian Halligan

Reputation: 21

Bit late to the party but after

cmd_query.ExecuteScalar()
MsgBox(cmd_query.LastInsertedId)

Produces the last insert id.

Upvotes: 2

blackriderws
blackriderws

Reputation: 853

You can use an double Query and use the function LAST_INSERT_ID() After run your first query to get the last current query:

Dim insert_coupon_query As String = ("INSERT INTO qa_discountcoupons (status_code) VALUES (5); SELECT LAST_INSERT_ID()")
                Dim cmd_query As New MySqlCommand(insert_coupon_query, objConn)
                Dim cmd_result As Integer = CInt(cmd_query.ExecuteScalar())

                MsgBox(cmd_result)

Upvotes: 14

sikander
sikander

Reputation: 2286

You can fetch the AUTO_INCREMENT value for a table through a MySQL query and then show that in your MsgBox

Upvotes: 1

Related Questions