Select @@Identity returns 0

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

Hello all -

I am trying to Insert a new record to an Access 2002 database.
Following the insert, I need to obtain the primary key of the inserted
row (person_ID) which is an Autonumber field. I am using the
following code (obtained from MSDN) to do this, but for some reason a
value of 0 is returned. Why is 0 returned????


Dim newID As Integer = 0
Dim idCMD As OleDbCommand = New OleDbCommand("SELECT @@IDENTITY",
cnConnection)

If e.StatementType = StatementType.Insert Then
newID = CInt(idCMD.ExecuteScalar())
e.Row("person_ID") = newID
End If


Also, the insert command is successful and does not cause any errors.

Thanks!
Brian
 
Brian,
You have to have the same connection and be using an AutoNumber field as
the PK for this to work in Access. The call needs to be be done immediately
after the insert occurs. Also, call e.Row.AcceptChanges() afterwards to
avoid having the row marked as having been edited. You also need to keep
the connection open between calls.
This type of call works just fine for me for both Access 2K and 2002.
Is this in a OleDbRowUpdatedEventHandler routine? Or just directly after
a manual insert? microsoft.public.dotnet.framework.adonet may be more
appropriate for this type of question.
Ron Allen
 
Hello all -

I am trying to Insert a new record to an Access 2002 database.
Following the insert, I need to obtain the primary key of the inserted
row (person_ID) which is an Autonumber field. I am using the
following code (obtained from MSDN) to do this, but for some reason a
value of 0 is returned. Why is 0 returned????


Dim newID As Integer = 0
Dim idCMD As OleDbCommand = New OleDbCommand("SELECT @@IDENTITY",
cnConnection)

If e.StatementType = StatementType.Insert Then
newID = CInt(idCMD.ExecuteScalar())
e.Row("person_ID") = newID
End If


Also, the insert command is successful and does not cause any errors.

Thanks!
Brian


Look in the SQL Help for SCOPE_IDENTITY. It may be what you are
looking for, it may not. Just thought i would stick my oar in and row
a bit.

:D

Don
 
Thanks Ron. I initially thought it might be an issue with the DB
connection so, thanks to your advice, I *think* I am now using the
same connection. But I am still getting 0 returned. Here's a bit
more of my code. Any additional help is greatly appreciated as I am
stumped on this one!

****
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuSave.Click

Try
cnConnection = fnOpenDPKDirectConn() ' opens connection to DB
bmcPersonMain.EndCurrentEdit()
daPersonMain.Update(dstPersonMain, "Person")
dstPersonMain.AcceptChanges()

MsgBox("Record saved successfully.", MsgBoxStyle.OKOnly)

Catch ex As Exception
MsgBox("Exception: " & (ex.ToString))
Finally
fnCloseDPKDirectConn(cnConnection)
End Try
End Sub
****
Private Sub daPersonMain_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles
daPersonMain.RowUpdated

Dim newID As Integer
Dim idCMD As OleDbCommand = New OleDbCommand("SELECT @@IDENTITY",
cnConnection)

If e.StatementType = StatementType.Insert Then
newID = CInt(idCMD.ExecuteScalar())
e.Row("person_ID") = newID
End If

End Sub
*****
 
Brian,
How about using e.Command.Connection for your identity select as this
will be the actual connection used for this row. Also are you sure that the
Access field is an AutoNumber field? When you trap in the debugger you are
getting 0 for newID after it is assigned, correct?

I'd also suggest getting a copy of ADO.NET Core Reference by David
Sceppa as it has some very clear explanations and samples in both C# and
VB.NET.

Ron Allen
 
That did the trick. Very interesting. I thank you for your help on
this. I just picked up a copy of ADO.NET Core Reference.
 
Back
Top