473,569 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TableAdapter Insert and SCOPE_IDENTITY

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 this value ?
Jun 8 '06 #1
3 9835
I believe it populates the table with the ID after the insert :)

Cheers,

Greg
"Ian Semmel" <is***********@ NOKUNKrocketcom p.com.au> wrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
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 this value ?

Jun 9 '06 #2
Problem is how to know which record in the table is the new one, as the only key
is the ID.

I looked around and found that there were some sql procedures that did it but
with my knowledge of sql somewhat limited I didn't fully understand them.

What I did was put a value in a field prior to the INSERT and then read the
table back in and found the record I needed.

Greg Young wrote:
I believe it populates the table with the ID after the insert :)

Cheers,

Greg
"Ian Semmel" <is***********@ NOKUNKrocketcom p.com.au> wrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
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
autoincrement ed ID.

Question. How do I get hold of this value ?


Jun 9 '06 #3


Ian Semmel wrote:
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 this value ?


In case anyone is interested, I finally got this to work

// Copy this code from Dataset.Designe r
string sql = "INSERT INTO ...... "
// BUT replace the SELECT statement in Designer with
+ "SELECT CAST(scope_iden tity() AS int)";

using ( SqlConnection connection = new SqlConnection ( connectionStrin g ) )
{
SqlCommand command = new SqlCommand ( sql, connection );

command.Command Type = System.Data.Com mandType.Text;

// This stuff is in Dataset.Designe r (sort of)
// Copy it changing the command identifier

command.Paramet ers.Add ( new System.Data.Sql Client.SqlParam eter (
"@parmName" , System.Data.Sql DbType.Int, 0,
System.Data.Par ameterDirection .Input, 0, 0, "ColumnName ",
System.Data.Dat aRowVersion.Cur rent, false, null, "", "", "" ) );
.....

command.Paramet ers [ 0 ].Value = ( (int) ( parmValue ) );
.....

connection.Open ();

int ID = (int) command.Execute Scalar ();
}
Jun 13 '06 #4

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

Similar topics

6
6497
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 table, I have to use SCOPE_IDENTITY(). Any ideas? SqlConnection conn = new SqlConnection(connectionString); conn.Open(); //Create the dataadapter
0
1922
by: jhansen | last post by:
I have a datagridview where a user enters a row of data. Upon hitting the ENTER key, I capture that event and add additional rows based upon business logic, and delete their original row. As this is a heads-down data entry app, it must be very speedy and this is not. All of my other trips back and forth to the server are quite rapid (i.e....
1
13805
by: gmccallum | last post by:
I would have expected to be able to find this easily, but I couldn't. I am using the TableAdapter.Insert command to insert a record. I would like to then get an ID field from that record after insertion. How to I retreive the record just inserted? Gregory McCallum gmccallum@honovi.com
2
3974
by: needin4mation | last post by:
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...
8
13927
by: Martin Z | last post by:
INSERT INTO dbo.Transmission (TransmissionDate, TransmissionDirection, Filename, TransmittedData) VALUES (@TransmissionDate,@TransmissionDirection,@Filename,@TransmittedData); SELECT @retVal = SCOPE_IDENTITY(); Pretty simple. There is an additional TransmissionID column that is an autonumber and primary key. @retVal is always null in my...
1
14354
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 database equivalent. How do i get that value from an InsertQuery call from TableAdapter generated by VS.NET?
3
11204
by: Randy | last post by:
I think that this is a really simple question. I have a table adapter set up with the various sql queries. I'd like to allow the user to make changes to a given row on a form. Each of the changes is made using textboxes (not a data grid). Once done, they click a Save button, which should call the insert query on the tableadapter. I can't...
0
1705
by: Yin99 | last post by:
I'm using TableAdapter with .NET 2.0 and SQL 2005. In SQL Database, a column's type is "float". Default TableAdapter methods update, insert, get, etc. maps this automatically to c# type "double" Question- Why is it when I create a new SQL Query, Insert Method, all of a sudden the tableadapter is calling the variable "float" when the...
3
1854
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 PersonName varchar(30) so after I edit the screen on 'add' mode, i call this cod here: PeopleBindingSource.EndEdit()
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
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...
1
7673
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...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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...
0
3653
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...
1
2113
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

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.