473,387 Members | 1,798 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.

get last inserted ID with

Hi all,

I have a database layer (DataSet) I use to attach my controls to. The
dataset is generated via the wizard. I want to know how I can get the
las inserted id I did with one of the queries I defined in that
databaselayer. I know of both @@IDENTITY and scope_identity() but I
can't get the id from that query.

I try:
int lastId = Convert.ToInt16(myDataSetTableAdaptor.GetLastIDQue ry());

where GetLastIDQuery() is the query that returns the last id ("SELECT
SCOPE_IDENTITY()") .

All help is greatly appreciated

Regards,
Stijn

Aug 29 '06 #1
3 4450
Stijn,

You have to do it in the same batch with the insert statement.

"INSERT ...;SELECT SCOPE_IDENTITY()"

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<ta******@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
Hi all,

I have a database layer (DataSet) I use to attach my controls to. The
dataset is generated via the wizard. I want to know how I can get the
las inserted id I did with one of the queries I defined in that
databaselayer. I know of both @@IDENTITY and scope_identity() but I
can't get the id from that query.

I try:
int lastId = Convert.ToInt16(myDataSetTableAdaptor.GetLastIDQue ry());

where GetLastIDQuery() is the query that returns the last id ("SELECT
SCOPE_IDENTITY()") .

All help is greatly appreciated

Regards,
Stijn

Aug 29 '06 #2
Dear Stijn

This may not be the most concise way of achiving it but I tend to use
Dataview objects with Dataset tables. You can use code that is easier
to follow with them.

e.g.

da.fill(ds)

'assume da, ds have been declared previously

dim dv as new DataView(ds.tables("tablename"))

dim drv as DataRowView = dv.AddNew

'Now load values into drv with statements like drv("columnName") =
value, except for the column that holds the identity which will be left
as null

drv.EndEdit()

da.Update(ds)

drv now holds the new identity in the appropriate column e.g. new_id =
drv("record_id")

Hope that's useful

Aug 29 '06 #3
Thanks, that worked. I also changed the query type to scalar.
Eliyahu Goldin schreef:
Stijn,

You have to do it in the same batch with the insert statement.

"INSERT ...;SELECT SCOPE_IDENTITY()"

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
<ta******@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
Hi all,

I have a database layer (DataSet) I use to attach my controls to. The
dataset is generated via the wizard. I want to know how I can get the
las inserted id I did with one of the queries I defined in that
databaselayer. I know of both @@IDENTITY and scope_identity() but I
can't get the id from that query.

I try:
int lastId = Convert.ToInt16(myDataSetTableAdaptor.GetLastIDQue ry());

where GetLastIDQuery() is the query that returns the last id ("SELECT
SCOPE_IDENTITY()") .

All help is greatly appreciated

Regards,
Stijn
Aug 29 '06 #4

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

Similar topics

1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
4
by: Carlos San Miguel | last post by:
Hi, I'm going to be using Oracle with a customer. I will access Oracle with ODBC. All the tables have an Identity column, this is the ID for the record. I need to know how I can get the value of...
1
by: Ann | last post by:
Hello everyone, hope someone can help me with this. I have a SQL stored procedure that inserts a record into a table, creates a cursor to fetch the last record that was added to get the unique...
3
by: siatki | last post by:
Hello, Firstly, sorry for my english. I have problem with creating SQL statement. I am beginner and I think that it is very easy to do. Look - I have to get only the last 20 records from table...
2
by: mgarriss | last post by:
Given this table: CREATE TABLE test ( id SERIAL, example TEXT ); An implicit sequence is created as show in this message: NOTICE: CREATE TABLE will create implicit sequence 'test_id_seq'...
6
by: Maurizio Faini | last post by:
I have a little problem. there is a way to get last id inserted into db or i have to make a new query? I explain better my question: in vbscript using sqlserver2000 i can use this code: ...
20
by: Guru | last post by:
Hi I have a table which contains number of rows. I want to fetch the last 5 records from the table. I know for the first 'n' records we can use FETCH FIRST n ROWS ONLY command. But i want to...
2
by: Daz | last post by:
Hi. I would like to know how to obtain a reference to (or at least, element type of) the last node which was inserted into the document. I am using an event listener to listen for dom inserts,...
13
rajiv07
by: rajiv07 | last post by:
Hi to all, I want to know how to select a last inserted record which is the primary key is not an integer. my table ramstr(Primary)----name--service XTC01-------Rajiv---service ...
20
by: canabatz | last post by:
hi all !! i got my results like that 1 user0 55 2 user1 53 3 user2 49 4 user2 48 5 user3 47 <------- last inserted row 6 user4 46
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.