473,748 Members | 7,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("S ELECT @@IDENTITY",
cnConnection)

If e.StatementType = StatementType.I nsert Then
newID = CInt(idCMD.Exec uteScalar())
e.Row("person_I D") = newID
End If
Also, the insert command is successful and does not cause any errors.

Thanks!
Brian
Nov 20 '05 #1
7 4102

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

---
Taiwo

"Brian" <br*********@ya hoo.com> wrote in message
news:be******** *************** ***@posting.goo gle.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("S ELECT @@IDENTITY",
cnConnection)

If e.StatementType = StatementType.I nsert Then
newID = CInt(idCMD.Exec uteScalar())
e.Row("person_I D") = 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.AcceptCha nges() 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 OleDbRowUpdated EventHandler routine? Or just directly after
a manual insert? microsoft.publi c.dotnet.framew ork.adonet may be more
appropriate for this type of question.
Ron Allen
"Brian" <br*********@ya hoo.com> wrote in message
news:be******** *************** ***@posting.goo gle.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("S ELECT @@IDENTITY",
cnConnection)

If e.StatementType = StatementType.I nsert Then
newID = CInt(idCMD.Exec uteScalar())
e.Row("person_I D") = newID
End If
Also, the insert command is successful and does not cause any errors.

Thanks!
Brian

Nov 20 '05 #3
br*********@yah oo.com (Brian) wrote in message news:<be******* *************** ****@posting.go ogle.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("S ELECT @@IDENTITY",
cnConnection)

If e.StatementType = StatementType.I nsert Then
newID = CInt(idCMD.Exec uteScalar())
e.Row("person_I D") = 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles mnuSave.Click

Try
cnConnection = fnOpenDPKDirect Conn() ' opens connection to DB
bmcPersonMain.E ndCurrentEdit()
daPersonMain.Up date(dstPersonM ain, "Person")
dstPersonMain.A cceptChanges()

MsgBox("Record saved successfully.", MsgBoxStyle.OKO nly)

Catch ex As Exception
MsgBox("Excepti on: " & (ex.ToString))
Finally
fnCloseDPKDirec tConn(cnConnect ion)
End Try
End Sub
****
Private Sub daPersonMain_Ro wUpdated(ByVal sender As Object, ByVal e As
System.Data.Ole Db.OleDbRowUpda tedEventArgs) Handles
daPersonMain.Ro wUpdated

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

If e.StatementType = StatementType.I nsert Then
newID = CInt(idCMD.Exec uteScalar())
e.Row("person_I D") = newID
End If

End Sub
*****
"Ron Allen" <ra****@src-us.com> wrote in message news:<uS******* *******@TK2MSFT NGP11.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.AcceptCha nges() 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 OleDbRowUpdated EventHandler routine? Or just directly after
a manual insert? microsoft.publi c.dotnet.framew ork.adonet may be more
appropriate for this type of question.
Ron Allen
"Brian" <br*********@ya hoo.com> wrote in message
news:be******** *************** ***@posting.goo gle.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("S ELECT @@IDENTITY",
cnConnection)

If e.StatementType = StatementType.I nsert Then
newID = CInt(idCMD.Exec uteScalar())
e.Row("person_I D") = 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.Conne ction 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*********@ya hoo.com> wrote in message
news:be******** *************** ***@posting.goo gle.com...
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles mnuSave.Click

Try
cnConnection = fnOpenDPKDirect Conn() ' opens connection to DB
bmcPersonMain.E ndCurrentEdit()
daPersonMain.Up date(dstPersonM ain, "Person")
dstPersonMain.A cceptChanges()

MsgBox("Record saved successfully.", MsgBoxStyle.OKO nly)

Catch ex As Exception
MsgBox("Excepti on: " & (ex.ToString))
Finally
fnCloseDPKDirec tConn(cnConnect ion)
End Try
End Sub
****
Private Sub daPersonMain_Ro wUpdated(ByVal sender As Object, ByVal e As
System.Data.Ole Db.OleDbRowUpda tedEventArgs) Handles
daPersonMain.Ro wUpdated

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

If e.StatementType = StatementType.I nsert Then
newID = CInt(idCMD.Exec uteScalar())
e.Row("person_I D") = newID
End If

End Sub
*****
"Ron Allen" <ra****@src-us.com> wrote in message

news:<uS******* *******@TK2MSFT NGP11.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.AcceptCha nges() 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 OleDbRowUpdated EventHandler routine? Or just directly after a manual insert? microsoft.publi c.dotnet.framew ork.adonet may be more
appropriate for this type of question.
Ron Allen
"Brian" <br*********@ya hoo.com> wrote in message
news:be******** *************** ***@posting.goo gle.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("S ELECT @@IDENTITY",
cnConnection)

If e.StatementType = StatementType.I nsert Then
newID = CInt(idCMD.Exec uteScalar())
e.Row("person_I D") = 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******* *******@TK2MSFT NGP10.phx.gbl>. ..
Brian,
How about using e.Command.Conne ction 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*********@ya hoo.com> wrote in message
news:be******** *************** ***@posting.goo gle.com...
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles mnuSave.Click

Try
cnConnection = fnOpenDPKDirect Conn() ' opens connection to DB
bmcPersonMain.E ndCurrentEdit()
daPersonMain.Up date(dstPersonM ain, "Person")
dstPersonMain.A cceptChanges()

MsgBox("Record saved successfully.", MsgBoxStyle.OKO nly)

Catch ex As Exception
MsgBox("Excepti on: " & (ex.ToString))
Finally
fnCloseDPKDirec tConn(cnConnect ion)
End Try
End Sub
****
Private Sub daPersonMain_Ro wUpdated(ByVal sender As Object, ByVal e As
System.Data.Ole Db.OleDbRowUpda tedEventArgs) Handles
daPersonMain.Ro wUpdated

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

If e.StatementType = StatementType.I nsert Then
newID = CInt(idCMD.Exec uteScalar())
e.Row("person_I D") = newID
End If

End Sub
*****
"Ron Allen" <ra****@src-us.com> wrote in message

news:<uS******* *******@TK2MSFT NGP11.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.AcceptCha nges() 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 OleDbRowUpdated EventHandler routine? Or just directly after a manual insert? microsoft.publi c.dotnet.framew ork.adonet may be more
appropriate for this type of question.
Ron Allen
"Brian" <br*********@ya hoo.com> wrote in message
news:be******** *************** ***@posting.goo gle.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("S ELECT @@IDENTITY",
> cnConnection)
>
> If e.StatementType = StatementType.I nsert Then
> newID = CInt(idCMD.Exec uteScalar())
> e.Row("person_I D") = 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*********@ya hoo.com> wrote in message
news:be******** *************** ***@posting.goo gle.com...
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******* *******@TK2MSFT NGP10.phx.gbl>. ..
Brian,
How about using e.Command.Conne ction 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5279
by: Karthik.S | last post by:
Does DB2 have a select @@ identity equivalent. Karthik
1
2835
by: Liming | last post by:
Hello all, I have a need to use "select @@identity" to get the autoid on the row that I just inserted. The insert store procedure executed correctly as if I took out the transaction, i can see the row being inserted. I just can't get back the id that's all. I use MS ACCESS btw (using a third party oledb add on, not sure if that's why it's causing problem) Database InvestmentsDb = DatabaseFactory.CreateDatabase( "Investments"
2
5578
by: Banski | last post by:
Hi, Im trying to use SELECT @@IDENTITY in a transaction. But it always returns 0. Im using an ms access database. And using the following code. What am i doing wrong? Best regards banski public int SavePage(int id....) {
2
3056
by: Beowulf | last post by:
If I run this statement in Query Analyzer, it properly returns 1 for my testing table. But if I put the statement into a stored procedure, the stored procedure returns NULL. What am I doing wrong? I suspect it may be related to how I defined the parameters for the stored procedure. Perhaps my definition of TableName and ColumnName don't match what COLUMNPROPERTY and OBJECT_ID expect to receive, but I don't know where to look for the...
4
11472
by: nsikkandar | last post by:
Greetings, I am in need of getting "Auto Generated Number" of Primary Key from master table (I am, using Access) when I insert a row through ADO.NET from VB.NET. Based on this ID, I want to generate new rows in child tables. When I was looking on the net to get some Idea, I found there is a feature SELECT @@IDENTITY to get "Auto Generated Number". But, I could not find clear details about this feature.
13
5798
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table CatalogImage where imgID also needs to be placed. Below is my code behind to carry the catID using the Select @@Identity and insert imgID to the bridge table. No data is being entered into the bridge table.
4
4299
by: Konrad Hammerer | last post by:
Hi! I have the following problem: I have a query (a) using another query (b) to get the amount of records of this other query (b), means: select count(MNR) as Number from Jahrbuch_Einzelversand_Komplett (while Jahrbuch_Einzelversand_Komplett is a query itself)
2
2180
by: alok | last post by:
Hi I have opened a serial port and then blocked on a select system call . but select returns even if no data is comming from other end. So when I read the buffer after select returns, I only found 0s nad this continues . So the problem is select returns even if there is no data in serial port buffer. void main() {
3
1752
by: cmrhema | last post by:
Hi, I have a table emp1 where i have two fields empno and empname. I have not assigned primary keys to any of these. After inserting values when I put on select @@identity or select @@identity from emp1 I always get the answer as NULL. Whereas it should return the maximum number of rows. What could be the problem. Kindly let me know.
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.