473,505 Members | 15,976 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I retrieve the scope_identity() with the tableadapter?

Hi, I have generated my queries and selected the refresh the table
option. This creates a select after the insert to get the
scope_identity() so that I can use that key value in another table,
i.e. foreign keys. I have verified the existence of the select in the
DAL .xsd file.

I simply do not know how to get that value, the scope_identity() value.
I know that it is selecting the value for me, but I have no idea how
to get that selected value after my insert. Am I supposed to get it in
my codefile somehow? Is there a way to use it in the DAL? Not sure
what the proper practice is here.

I have seen several places where stored procedures were turned to.
This does not make sense, totally, to me, but more like a workaround.
It seems that if I have an option to generate the sql to get the ID
field, then there must be a way to access the data. I don't mind
coding, but I do not want a sproc (no offense to sproc advocates. I
just want to see how to do this dynamically with Visual Studio 2005.)

Thank you for any help.

Oct 12 '06 #1
2 3970
Is the only way to use the scalar value that is returned by breaking
out of the DAL and using code behind to grab the value?

ne***********@gmail.com wrote:
Hi, I have generated my queries and selected the refresh the table
option. This creates a select after the insert to get the
scope_identity() so that I can use that key value in another table,
i.e. foreign keys. I have verified the existence of the select in the
DAL .xsd file.

I simply do not know how to get that value, the scope_identity() value.
I know that it is selecting the value for me, but I have no idea how
to get that selected value after my insert. Am I supposed to get it in
my codefile somehow? Is there a way to use it in the DAL? Not sure
what the proper practice is here.

I have seen several places where stored procedures were turned to.
This does not make sense, totally, to me, but more like a workaround.
It seems that if I have an option to generate the sql to get the ID
field, then there must be a way to access the data. I don't mind
coding, but I do not want a sproc (no offense to sproc advocates. I
just want to see how to do this dynamically with Visual Studio 2005.)

Thank you for any help.
Oct 13 '06 #2
I found that I could simply enter more SQL in the DAL to accomplish
this like so:

After my initial insert in the database:

INSERT INTO table2 ([fk_employee_id]) SELECT EmployeeID FROM Employees
WHERE (EmployeeID = SCOPE_IDENTITY())

I put this in the xsd after my INSERT. To cascade deletes, I add an
additional delete to the delete command, etc.

ne***********@gmail.com wrote:
Is the only way to use the scalar value that is returned by breaking
out of the DAL and using code behind to grab the value?

ne***********@gmail.com wrote:
Hi, I have generated my queries and selected the refresh the table
option. This creates a select after the insert to get the
scope_identity() so that I can use that key value in another table,
i.e. foreign keys. I have verified the existence of the select in the
DAL .xsd file.

I simply do not know how to get that value, the scope_identity() value.
I know that it is selecting the value for me, but I have no idea how
to get that selected value after my insert. Am I supposed to get it in
my codefile somehow? Is there a way to use it in the DAL? Not sure
what the proper practice is here.

I have seen several places where stored procedures were turned to.
This does not make sense, totally, to me, but more like a workaround.
It seems that if I have an option to generate the sql to get the ID
field, then there must be a way to access the data. I don't mind
coding, but I do not want a sproc (no offense to sproc advocates. I
just want to see how to do this dynamically with Visual Studio 2005.)

Thank you for any help.
Oct 13 '06 #3

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

Similar topics

4
23706
by: Scrappy | last post by:
I have an ASP front end on SQL 2000 database. I have a form that submits to an insert query. The entry field is an "identity" and the primary key. I have used scope_identity() to display the...
2
5448
by: WhiteEagl | last post by:
Hello, I would need some help with this identity column problem in SQLServer. I have a database with two tables. The Parent table has an Identity column. Parent (ParentID IDENTITY, Name)...
3
1691
by: Mark | last post by:
Hi, I'm having a bit of difficulty in getting the identity of a new row using the SCOPE_IDENTITY() function in SQLServer 2000. I've added the following code: 'set @LastID = SCOPE_IDENTITY()' to...
6
6473
by: Hardy Wang | last post by:
Hi all, I have the following codes, but SCOPE_IDENTITY() just returns NULL to me. If I comment out SCOPE_IDENTITY() line and run @@IDENTITY line, it works fine!! Since I have a trigger on the...
3
9831
by: Ian Semmel | last post by:
The generated INSERT statement in a typed dataset has an INSERT statement followed by a SELECT statement which gets (I think) the value of the autoincremented ID. Question. How do I get hold of...
8
13920
by: Martin Z | last post by:
INSERT INTO dbo.Transmission (TransmissionDate, TransmissionDirection, Filename, TransmittedData) VALUES (@TransmissionDate,@TransmissionDirection,@Filename,@TransmittedData); SELECT @retVal =...
1
14328
by: =?Utf-8?B?RGV2YW4=?= | last post by:
Hi, I have a table adapter for a database with an insert query. I want to be able to get the PRIMARY key of the inserted record for that insert statement. I know that SCOPE_IDENTITY is the...
10
4025
by: Mike | last post by:
Sql Server, Scope_Identity(), Ado.NET: Which is better? Using an output parameter to return Scope_Identity through ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the...
3
1843
by: Miro | last post by:
I am trying to get my ID added from a people table in an access table "MyContacts.mdb" i just added. Its basically a contact file i created in access: PK Person_ID - Unique - autoincrement...
0
7218
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
7103
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
7307
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,...
1
7021
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
5614
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,...
1
5035
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...
0
3188
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...
0
1532
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 ...
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.