Getting Unique ID from new record

G

Glyn Williams

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,Model,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
 
G

Guest

Here's a sample that may be helpful

http://www.fawcette.com/vsm/2002_11/online/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,Model,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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top