473,387 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

SELECT @@IDENTITY

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.

I did the following code and executed, I am getting the result. But, I
am not sure, what I am doing is right. I would be thankful, if any one,
just go through my below code and let me know it is correct or it may
cause some error at some circumstances such as when multiple user
trying to execute the application and fire this method.

<-----Code Start Here ------->

Dim NewID As Integer

Public Shared Function ExecuteNonQuery(ByVal query As String) As
Boolean

Dim ConnectionString As String =
System.Configuration.ConfigurationSettings.AppSett ings("DSN") '
Declared in App.Config File for Connection String

Dim myConnection As New OleDbConnection(ConnectionString)

Try

myConnection.Open()

' Argument for the query is ""insert into Candidate_Personal
(Name,FatherName,DateOfBirth,PlaceOfBirth,Address, City,Pincode,Phone,Mobile,Email)
values ('" & txtCandName.Text & "', '" & txtCandFName.Text & "', '" &
txtCandDoB.Text & "', '" & txtCandPoB.Text & "', '" &
txtCandAddress.Text & "', '" & txtCandCity.Text & "', '" &
txtCandPincode.Text & "', '" & txtCandPhone.Text & "', '" &
txtCandMobile.Text & "', '" & txtCandEmail.Text & "') "

'Note that the table Candidate_Personal contains Autogenerated Primary
Key called ID.

Dim myCommand As New OleDbCommand(query, myConnection)

Dim myCommand1 As New OleDbCommand("SELECT @@IDENTITY",
myConnection)
myCommand.ExecuteNonQuery()
myCommand1.ExecuteNonQuery()
NewID = myCommand1.ExecuteScalar
'MsgBox("The Candidate ID Generated by the system is: " &
NewID)
Return True
Catch ex As Exception
MsgBox("Experiencing Technical Problem! Error at
DataManager.ExecuteNonQuery" & vbNewLine & ex.Message)
Return False
Finally
If myConnection.State = ConnectionState.Open Then
myConnection.Close()
End If
End Try
End Function
<---- Code End Here ---->

Many Thanks in advance

Sikkandar

Mar 2 '06 #1
4 11382
I would make the insert command and the select @@identity command all
part of the same sql command, and run it as a datareader query. This
insures that you will get back the identity of the row YOU inserted.

Mar 2 '06 #2
<ns********@gmail.com> schrieb:
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.


HOW TO: Retrieve the Identity Value While Inserting Records into Access
Database By Using Visual C# .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;816112>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Mar 2 '06 #3
The SELECT @@IDENTITY returns a scalar value.... you'll want to execute your
command for it using .ExecuteScalar and putting it into a variable:

Dim NewID As Int32

NewID = myCommand1.ExecuteScalar

... Oh wait, I see that you are, but just before that you execute a
..ExecuteNonQuery... drop that line, it's probably what is causing the
interference.

Other than that, it looks like it should work. One item of caution, it's
generaly a bad idea to concatenate SQL strings together like that. That is
how SQL Injection hacks develop. A Parameterized Query would work best.

-Chris

"ns********@gmail.com" wrote:
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.

I did the following code and executed, I am getting the result. But, I
am not sure, what I am doing is right. I would be thankful, if any one,
just go through my below code and let me know it is correct or it may
cause some error at some circumstances such as when multiple user
trying to execute the application and fire this method.

<-----Code Start Here ------->

Dim NewID As Integer

Public Shared Function ExecuteNonQuery(ByVal query As String) As
Boolean

Dim ConnectionString As String =
System.Configuration.ConfigurationSettings.AppSett ings("DSN") '
Declared in App.Config File for Connection String

Dim myConnection As New OleDbConnection(ConnectionString)

Try

myConnection.Open()

' Argument for the query is ""insert into Candidate_Personal
(Name,FatherName,DateOfBirth,PlaceOfBirth,Address, City,Pincode,Phone,Mobile,Email)
values ('" & txtCandName.Text & "', '" & txtCandFName.Text & "', '" &
txtCandDoB.Text & "', '" & txtCandPoB.Text & "', '" &
txtCandAddress.Text & "', '" & txtCandCity.Text & "', '" &
txtCandPincode.Text & "', '" & txtCandPhone.Text & "', '" &
txtCandMobile.Text & "', '" & txtCandEmail.Text & "') "

'Note that the table Candidate_Personal contains Autogenerated Primary
Key called ID.

Dim myCommand As New OleDbCommand(query, myConnection)

Dim myCommand1 As New OleDbCommand("SELECT @@IDENTITY",
myConnection)
myCommand.ExecuteNonQuery()
myCommand1.ExecuteNonQuery()
NewID = myCommand1.ExecuteScalar
'MsgBox("The Candidate ID Generated by the system is: " &
NewID)
Return True
Catch ex As Exception
MsgBox("Experiencing Technical Problem! Error at
DataManager.ExecuteNonQuery" & vbNewLine & ex.Message)
Return False
Finally
If myConnection.State = ConnectionState.Open Then
myConnection.Close()
End If
End Try
End Function
<---- Code End Here ---->

Many Thanks in advance

Sikkandar

Mar 2 '06 #4
Wagner:

In your given link, they are getting "AutoIncreamentID" in the event of
"RowUpdated" which needs more coding. It seems, will be in safe side.

Zaks:

As per the link, given by Mr. Wagner and other references, we should
use another OleDbCommand for SELECT @@IDENTITY.

Your concept is NOT clear for me. Could you please explain your logic
with little sample code. ?

Chris Anderson,

Good catch. I dropped the line "myCommand1.ExecuteNonQuery()". I agree
with your suggession. But, I just took it as a scenario, when we are
forced to do such coding.

Once again many thanks guys.

-Sikkandar.

Mar 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I...
2
by: Karthik.S | last post by:
Does DB2 have a select @@ identity equivalent. Karthik
17
by: Trevor Best | last post by:
I don't know if this has been reported before but it appears to be a bug with Access. If I create two tables both with an identity column then create an insert trigger on table1 that inserts a...
1
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...
2
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 ...
3
by: Dan | last post by:
I'm writing a record from an asp.net page to SQL Server. After the insert I'm selecting @@identity to return the ID of the record that I just wrote. It worked fine until I typed a semicolon into...
7
by: Brian | last post by:
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...
13
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...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...

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.