473,623 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ 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.SqlCo nnection
MyConn.Connecti onString = g_SQLConnStr
Dim Mycommand As New SqlClient.SqlCo mmand
Try
Dim NewRecordPK As Integer
Mycommand.Comma ndText = "Insert into MyTable (Data1,Data2) " & _
"VALUES ('Data1Value',' Data2Value')
Mycommand.Conne ction = MyConn
MyConn.Open()
NewRecordPK = Mycommand.Execu teScalar()

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 2175
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******** *****@TK2MSFTNG P05.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.SqlCo nnection
MyConn.Connecti onString = g_SQLConnStr
Dim Mycommand As New SqlClient.SqlCo mmand
Try
Dim NewRecordPK As Integer
Mycommand.Comma ndText = "Insert into MyTable (Data1,Data2) " &
_
"VALUES ('Data1Value',' Data2Value')
Mycommand.Conne ction = MyConn
MyConn.Open()
NewRecordPK = Mycommand.Execu teScalar()

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
3337
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 columns, "scode" and "sname", as 1 column (if not possible then 2 columns will be fine) in the listbox. Can the listbox display 2 columns information from the dataset and how can I do that? Also, I set the property of the listbox to selectionmode...
1
1164
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? Sorry about the cross group post, but I'm not sure where and who would know this. Thanks! -- David Lozzi Web Applications/Network Specialist Delphi Technology Solutions, Inc. dlozzi(remove-this)@delphi-ts.com
4
2409
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 (setting breakpoint in page_load()). I'm getting the following message: "Unable to start debugging on the web server. An error occurred that usually indicates a corrupt installation (code 0x80004002). If the problem persists,
2
1359
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 crash of my Windows 2003 server I was in a hurry creating a very quick backup so I didn't tokk the time to find out what a correct backup mechanisme shoul be. Now I have a new server and installed the latest MYSQL (I think I had an
2
2285
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 Info=False;" & _ "Workstation ID=MyWS"
0
854
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 to the new row? I have created a method that calls the AddNew method on a DefaultView and creates a new row. I need to move to that newly created row and get the value from a auto-incremented cell in the new row and then populate a combo box with...
22
1679
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 debugging for now. Did I mess up all these quotes? // once again this is the key line of code, problem area, disregard
3
1777
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 user to pick a third form to be created. Once this third from is created i want to add this newly created object back to an object list on the main form that is linked to the datagrid. Any help would be much appreciated. It is holdning me up on a...
2
1324
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 back-up of the primary back-end on that front-end's local network; so that in the event the link is lost to the primary back-end, that location's will automatically connect to the back-up back-end. Confused yet, good me too. My delima is that when...
0
8224
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8667
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8610
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8324
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8469
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6104
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4070
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2597
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
1
1775
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.