473,487 Members | 2,483 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I get back a newly created Primary key

Using Vs2005. In this snippet of code I would be creating a new record in a
sql server 2000 database table. How do I obtain the value of primary key -
field name PK - for the newly created record. The field PK is an identity
field that automatically increments its value.

Dim MyConn As New SqlClient.SqlConnection
MyConn.ConnectionString = g_SQLConnStr
Dim Mycommand As New SqlClient.SqlCommand
Try
Dim NewRecordPK As Integer
Mycommand.CommandText = "Insert into MyTable (Data1,Data2) " & _
"VALUES ('Data1Value','Data2Value')
Mycommand.Connection = MyConn
MyConn.Open()
NewRecordPK = Mycommand.ExecuteScalar()

etc....

I thought that ExecuteScalar would return the value of the first field of
the affected record but it always returns 0, yet I can see in the table that
the record has been added OK with a PK value other than 0, ie it increments
correctly.

Any help would be greatly appreciated,
Bob
Oct 21 '06 #1
3 2156
Try to add at your SQL query, a Return @@IDENTITY ...

--
Tiago Salgado // http://weblogs.pontonetpt.com/tiagosalgado

On Sat, 21 Oct 2006 20:39:42 +0100, rdufour <bd*****@sgiims.comwrote:
ted,

Oct 21 '06 #2
If you are using SQL Server 2005, you can include the OUTPUT clause in your
SQL statement. Assuming that the name of your primary key field is PK, use
this statement.

INSERT INTO MyTable (Data1,Data2)
OUTPUT INSERTED.PK
VALUES ('Data1Value','Data2Value')

-----
Tim Patrick
Start-to-Finish Visual Basic 2005
Oct 21 '06 #3
Thanks to both of you.
Bob
"rdufour" <bd*****@sgiims.comwrote in message
news:un*************@TK2MSFTNGP05.phx.gbl...
Using Vs2005. In this snippet of code I would be creating a new record in
a sql server 2000 database table. How do I obtain the value of primary
key - field name PK - for the newly created record. The field PK is an
identity field that automatically increments its value.

Dim MyConn As New SqlClient.SqlConnection
MyConn.ConnectionString = g_SQLConnStr
Dim Mycommand As New SqlClient.SqlCommand
Try
Dim NewRecordPK As Integer
Mycommand.CommandText = "Insert into MyTable (Data1,Data2) " &
_
"VALUES ('Data1Value','Data2Value')
Mycommand.Connection = MyConn
MyConn.Open()
NewRecordPK = Mycommand.ExecuteScalar()

etc....

I thought that ExecuteScalar would return the value of the first field of
the affected record but it always returns 0, yet I can see in the table
that the record has been added OK with a PK value other than 0, ie it
increments correctly.

Any help would be greatly appreciated,
Bob

Oct 22 '06 #4

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

Similar topics

18
3321
by: Alpha | last post by:
Hi, I'm working on a Windows applicaton with VS 2003 on windows 2000. I have a listbox that I have binded to a dataset table, "source" which has 3 columns. I would like to display 2 of those...
1
1156
by: David Lozzi | last post by:
I'm creating new records in my aspx file, but after its created, I need the ID of the new record. The ID field is the primary key and increments automatically. Should this be completed in a proc?...
4
2397
by: mcwooq | last post by:
Hi, I just installed the VS.Studio 2005 Team Edition for Developper and encountered severe problems with debugging ASP.NET 2.0 projects. Even newly empty created ASP 2.0 projects can't debug...
2
1349
by: Erik van der Veen | last post by:
Hi, Can anybody tell me how I can get the backup ( just a file copy of the directory with .frm, .MYI and .MYD files) back into a newly created database. I am failry new to MySQL. Due to a...
2
2269
by: Bob | last post by:
This is the snippet of code Dim myConnString As String = "Integrated Security=SSPI;Packet Size=4096;Data Source=MyServer;" & _ "Initial Catalog=MyDatabase; " & _ "Persist Security...
0
842
by: Chris B | last post by:
Howdy, When the AddNew button is clicked on the BindingNavigator, a new row is made in the datagrid and the row selector moves to that newly created row, what is the method that moves the selector...
22
1665
by: DL | last post by:
Hi, What I wanted to do is to call a function from a newly created element. But it stumbled me. Here's the line that references the newly created element and I used the alert function for...
3
1774
by: DragonLord | last post by:
I have a scenario i am not sure how to code it. Basically i have a Main form that has a datagrid of objects. I want to click a button on this form that will load a second form that will allow the...
2
1310
jmoudy77
by: jmoudy77 | last post by:
Hi, I've got a database with front-ends at multiple locations that pull from a primary back-end over a satellite network. Each time a front-end closes it triggers a back-up utility that creates a...
0
7108
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
7142
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
7181
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...
1
6847
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
7352
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...
0
5445
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,...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.