473,503 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting Unique ID from new record

Hi,
I'm trying to get the ID of a newly created record into a variable, so I can
display it in a Label or something after being created. I know you can use
@@identity in the sql statement, but I just can't seem to get it to go.

Any help appreciated,
Glyn Williams

Here is my code:-
Public Sub addBike(Sender As Object, E As EventArgs)

Dim myCommand As new SqlCommand()
Dim myConnection As New SqlConnection
("server=Tron;database=sellmybike;Integrated Security=SSPI")

myCommand.Connection = myConnection
'Define insert command
myCommand.CommandText="INSERT INTO Details
(PostDate,Name,Town,County,Seller,Email,Tel,Make,M odel,ManYear,Mileage,Price
,Details) VALUES (
@PostDate,@Name,@Town,@County,@Seller,@Email,@Tel, @Make,@Model,@ManYear,@Mil
eage,@Price,@Details)"

myCommand.Parameters.Add ("@PostDate", Now())
myCommand.Parameters.Add ("@Name", msgName.Value)
myCommand.Parameters.Add ("@Town", msgTown.Value)
myCommand.Parameters.Add ("@County", msgCounty.Value)
myCommand.Parameters.Add ("@Seller", msgSeller.Value)
myCommand.Parameters.Add ("@Email", msgEmail.Value)
myCommand.Parameters.Add ("@Tel", msgTel.Value)
myCommand.Parameters.Add ("@Make", msgMake.Value)
myCommand.Parameters.Add ("@Model", msgModel.Value)
myCommand.Parameters.Add ("@ManYear", msgManYear.Value)
myCommand.Parameters.Add ("@Mileage", msgMileage.Value)
myCommand.Parameters.Add ("@Price", msgPrice.Value)
myCommand.Parameters.Add ("@Details", msgDetails.Value)

myCommand.Connection.Open()
'update the tape
myCommand.ExecuteNonQuery()

'Im sure some code must go here and in the sql statement

myCommand.Connection.Close()
End Sub

Nov 18 '05 #1
1 1126
Here's a sample that may be helpful

http://www.fawcette.com/vsm/2002_11/.../hottips/rehak

----- Glyn Williams wrote: ----

Hi
I'm trying to get the ID of a newly created record into a variable, so I ca
display it in a Label or something after being created. I know you can us
@@identity in the sql statement, but I just can't seem to get it to go

Any help appreciated
Glyn William

Here is my code:
Public Sub addBike(Sender As Object, E As EventArgs

Dim myCommand As new SqlCommand(
Dim myConnection As New SqlConnectio
("server=Tron;database=sellmybike;Integrated Security=SSPI"

myCommand.Connection = myConnectio
'Define insert comman
myCommand.CommandText="INSERT INTO Detail
(PostDate,Name,Town,County,Seller,Email,Tel,Make,M odel,ManYear,Mileage,Pric
,Details) VALUES
@PostDate,@Name,@Town,@County,@Seller,@Email,@Tel, @Make,@Model,@ManYear,@Mi
eage,@Price,@Details)

myCommand.Parameters.Add ("@PostDate", Now()
myCommand.Parameters.Add ("@Name", msgName.Value
myCommand.Parameters.Add ("@Town", msgTown.Value
myCommand.Parameters.Add ("@County", msgCounty.Value
myCommand.Parameters.Add ("@Seller", msgSeller.Value
myCommand.Parameters.Add ("@Email", msgEmail.Value
myCommand.Parameters.Add ("@Tel", msgTel.Value
myCommand.Parameters.Add ("@Make", msgMake.Value
myCommand.Parameters.Add ("@Model", msgModel.Value
myCommand.Parameters.Add ("@ManYear", msgManYear.Value
myCommand.Parameters.Add ("@Mileage", msgMileage.Value
myCommand.Parameters.Add ("@Price", msgPrice.Value
myCommand.Parameters.Add ("@Details", msgDetails.Value

myCommand.Connection.Open(
'update the tap
myCommand.ExecuteNonQuery(

'Im sure some code must go here and in the sql statemen

myCommand.Connection.Close(
End Su


Nov 18 '05 #2

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

Similar topics

7
2381
by: Tony Clarke | last post by:
Hi, I'm trying to write a system thats used for about 50 clients that uses html forms and php to log details. The problem is that when a client loads the form page it's given a value which is...
6
1732
by: Billy Cormic | last post by:
Hello, I would like do an insert into a table. The table has an auto incrimenting unique int id. After I do the insert how do i get the unique int id of the record that I just inserted? Is...
5
11515
by: Dave Sisk | last post by:
Hey folks: I'm trying to do this: CREATE TRIGGER datawhse.emp_ti AFTER INSERT ON emp REFERENCING NEW AS n FOR EACH ROW MODE DB2ROW BEGIN DECLARE v_rrn DECIMAL(15,0);
2
7792
by: SJM | last post by:
I have a report that displays records of real estate properties. It is possible for each property to appear a number of times for various reasons. Each record however is unique. What I would like...
15
2943
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
12
1723
by: Lennart Anderson | last post by:
I'm having a MySQl table wih a lot of information. I want to present some main fields in a table on one page. Each record do, of course, have a unique ID. The presnted table will have one field as...
2
1111
by: JJ | last post by:
OK - I asked this earlier but thought I'd solved it. Clearly not. I have a control that I need to display text from an sql database. The control is being used as a web part so can be placed on a...
12
5556
by: magmike | last post by:
Accidentally deleted a record. Anyway to get it back? If not, I know the ID number - which is an autonumber field. Because of the related data from other tables, would I be able to create a new...
2
3883
by: banderson | last post by:
Hello, I have a data entry form for a table with information about buildings and am having a problem making a combo box do what I want. I would like to make the combo box show a list of unique bldg...
9
1736
by: KDawg44 | last post by:
Hi, I have PHP function that adds a record to the database. The table has an ID that is AUTO_INCREMENT. Is there anyway to get that ID back when I do any kind of insert? That ID is a foreign...
0
7205
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
7093
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
7287
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
7006
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
7467
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
5592
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
4685
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
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
744
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.