473,396 Members | 2,020 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,396 software developers and data experts.

Best way to get ID of inserted row??

Hi, I do a sqlcommand.executenonquery on the click of a button, which
performs an insert statement. What is the most reliable way (i.e. someone
else could insert at similar time) to get the id i have JUST created?
Nov 18 '05 #1
4 7139
Set a counter at another table.

When you need new number, use a while loop to select the counter then
increment the counter by 1 where it's value equal to the origional value. If
returned affected number of rows = 0, you know the number has been used by
the others then you should get the next one......

"louise raisbeck" <lo************@discussions.microsoft.com> ¦b¶l¥ó
news:25**********************************@microsof t.com ¤¤¼¶¼g...
Hi, I do a sqlcommand.executenonquery on the click of a button, which
performs an insert statement. What is the most reliable way (i.e. someone
else could insert at similar time) to get the id i have JUST created?

Nov 18 '05 #2
Louise,

Use ExecuteScalar wchich returns the value of first column in the first row,
and use the scope identity function to get the identity value you have just
inserted. Below is some code to give you an idea.

create table #temp
( myid int identity,
n varchar(10)
)

insert into #temp
values('a')

select scope_identity() as [identity_value]

--
With Regards,
Deepak
[I code, therefore I am]

"louise raisbeck" <lo************@discussions.microsoft.com> wrote in
message news:25**********************************@microsof t.com...
Hi, I do a sqlcommand.executenonquery on the click of a button, which
performs an insert statement. What is the most reliable way (i.e. someone
else could insert at similar time) to get the id i have JUST created?

Nov 18 '05 #3
Thanks.

int intNewID = Convert.ToInt32(command.ExecuteScalar());

I put select_scopeidentity() onto the end of my insert statement, ran the
above code and obtained my new ID. it runs in the same connection so the ID
shouldnt belong to someone else's query, should it?

"Deepak" wrote:
Louise,

Use ExecuteScalar wchich returns the value of first column in the first row,
and use the scope identity function to get the identity value you have just
inserted. Below is some code to give you an idea.

create table #temp
( myid int identity,
n varchar(10)
)

insert into #temp
values('a')

select scope_identity() as [identity_value]

--
With Regards,
Deepak
[I code, therefore I am]

"louise raisbeck" <lo************@discussions.microsoft.com> wrote in
message news:25**********************************@microsof t.com...
Hi, I do a sqlcommand.executenonquery on the click of a button, which
performs an insert statement. What is the most reliable way (i.e. someone
else could insert at similar time) to get the id i have JUST created?


Nov 18 '05 #4
Thats right, this ID value has now been used and will not be available to
any other insert.

--
With Regards,
Deepak
[I code, therefore I am]

"louise raisbeck" <lo************@discussions.microsoft.com> wrote in
message news:CA**********************************@microsof t.com...
Thanks.

int intNewID = Convert.ToInt32(command.ExecuteScalar());

I put select_scopeidentity() onto the end of my insert statement, ran the
above code and obtained my new ID. it runs in the same connection so the
ID
shouldnt belong to someone else's query, should it?

"Deepak" wrote:
Louise,

Use ExecuteScalar wchich returns the value of first column in the first
row,
and use the scope identity function to get the identity value you have
just
inserted. Below is some code to give you an idea.

create table #temp
( myid int identity,
n varchar(10)
)

insert into #temp
values('a')

select scope_identity() as [identity_value]

--
With Regards,
Deepak
[I code, therefore I am]

"louise raisbeck" <lo************@discussions.microsoft.com> wrote in
message news:25**********************************@microsof t.com...
> Hi, I do a sqlcommand.executenonquery on the click of a button, which
> performs an insert statement. What is the most reliable way (i.e.
> someone
> else could insert at similar time) to get the id i have JUST created?


Nov 18 '05 #5

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

Similar topics

11
by: Harlin Seritt | last post by:
There are certain options for Tkinter widgets that have default values that I don't much care for (borderwidth, font come to mind) and continuously change when I'm building interfaces. With a bit...
2
by: george | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, on an NT box, Active Server pages with Javascript, using ADO objects. ...
16
by: LP | last post by:
Hi, Every morning a .NET application downloads a file with cumulative data which needs to be appended to SQL Server table. This program needs to identify records that have not been previously...
10
by: jaYPee | last post by:
I have a function that call a stored procedure which performs an insert command. now i want to refresh the dataset so that the newly inserted data will be available to my datagrid I have tried...
1
by: j_mckitrick | last post by:
Hi all, I'm making Ajax-type calls that return XML, and rather than parsing the XML with DOM and building the nodes by hand, it seems much better and well-designed to get the XML response, get...
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
2
by: rn5a | last post by:
A Form has a select list which is populated from a MS-Access database table. The DB table from where the select list is populated has 2 columns - CountryID & CountryName. When the Form is posted,...
4
ck9663
by: ck9663 | last post by:
hi guys this is a little challenging, at least for me...here goes... i have to run a DOS batch file from a server. with some parameters that i need to pass. these parameters can be found on a...
1
by: whizkid | last post by:
Hi... Looking for a solution to a problem that I am facing... Basically I have a table schema defined as follows. Table X( br char(2), Dealno char(8) SeqNo char(4)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.