By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,674 Members | 1,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,674 IT Pros & Developers. It's quick & easy.

Select @@Identity returns 0

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

I don't believe "SELECT @@IDENTITY" works for Access, but it works for SQL
Server.

---
Taiwo

"Brian" <br*********@yahoo.com> wrote in message
news:be**************************@posting.google.c om...
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

Nov 20 '05 #2

P: n/a
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
"Brian" <br*********@yahoo.com> wrote in message
news:be**************************@posting.google.c om...
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

Nov 20 '05 #3

P: n/a
br*********@yahoo.com (Brian) wrote in message news:<be**************************@posting.google. com>...
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
Nov 20 '05 #4

P: n/a
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
*****
"Ron Allen" <ra****@src-us.com> wrote in message news:<uS**************@TK2MSFTNGP11.phx.gbl>...
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
"Brian" <br*********@yahoo.com> wrote in message
news:be**************************@posting.google.c om...
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

Nov 20 '05 #5

P: n/a
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
"Brian" <br*********@yahoo.com> wrote in message
news:be**************************@posting.google.c om...
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
*****
"Ron Allen" <ra****@src-us.com> wrote in message

news:<uS**************@TK2MSFTNGP11.phx.gbl>...
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
"Brian" <br*********@yahoo.com> wrote in message
news:be**************************@posting.google.c om...
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

Nov 20 '05 #6

P: n/a
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.
"Ron Allen" <ra****@src-us.com> wrote in message news:<eB**************@TK2MSFTNGP10.phx.gbl>...
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
"Brian" <br*********@yahoo.com> wrote in message
news:be**************************@posting.google.c om...
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
*****
"Ron Allen" <ra****@src-us.com> wrote in message

news:<uS**************@TK2MSFTNGP11.phx.gbl>...
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
"Brian" <br*********@yahoo.com> wrote in message
news:be**************************@posting.google.c om...
> 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

Nov 20 '05 #7

P: n/a
Brian,
I'm glad I could be of some help.
Ron Allen
"Brian" <br*********@yahoo.com> wrote in message
news:be**************************@posting.google.c om...
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.
"Ron Allen" <ra****@src-us.com> wrote in message

news:<eB**************@TK2MSFTNGP10.phx.gbl>...
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.

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.