472,119 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Select @@Identity returns 0

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
7 3995

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
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
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
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
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
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
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.

Similar topics

2 posts views Thread by Karthik.S | last post: by
1 post views Thread by Liming | last post: by
2 posts views Thread by Banski | last post: by
4 posts views Thread by nsikkandar | last post: by
13 posts views Thread by PinkBishop | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.