|
-
Aug 12th, 2009, 07:36 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Display @id=scope_identity()"
Code:
Dim SQLData As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("FixedLineProvisioningConnectionString").ToString())
Dim cmdSelect As New System.Data.SqlClient.SqlCommand("INSERT INTO tblTotalComms_custinfo SET nt_username=@nt_username; SELECT @ID=SCOPE_IDENTITY()", SQLData)
cmdSelect.Parameters.AddWithValue("@nt_username", Page.User.Identity.Name)
cmdSelect.Parameters.Add("@ID", System.Data.SqlDbType.Int, 4, "ID").Direction = Data.ParameterDirection.Output
SQLData.Open()
SQLData.Close()
w.HeaderText = "Order Number " & Session("OrderNo")
I want to return the value @ID from the insert statement. Does anybody know how i would achieve this?
-
Aug 12th, 2009, 07:58 AM
#2
Re: Display @id=scope_identity()"
Code:
;select scope_identity()
example:
Code:
Dim query As String = "INSERT INTO tbl_items (item) Values (?);Select scope_identity()"
Dim ID As Integer
Dim connect As String = "sql connection string"
Using conn As New SqlConnection(connect)
Using cmd As New SqlCommand(query, conn)
cmd.Parameters.AddWithValue("", YOURVALUE)
conn.Open()
ID = cmd.ExecuteScalar()
End Using
End Using
i didn't test it but it should work, if not post back
Last edited by motil; Aug 12th, 2009 at 08:08 AM.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Aug 12th, 2009, 08:38 AM
#3
Thread Starter
Addicted Member
Re: Display @id=scope_identity()"
"Incorrect syntax near the keyword 'SET'."
Pointing at line... ID = cmd.ExecuteScalar()
-
Aug 12th, 2009, 08:39 AM
#4
Re: Display @id=scope_identity()"
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Aug 12th, 2009, 08:42 AM
#5
Thread Starter
Addicted Member
Re: Display @id=scope_identity()"
Code:
Dim query As String = "INSERT INTO tblTotalComms_custinfo SET nt_username=@nt_username; SELECT SCOPE_IDENTITY()"
Dim ID As Integer
Dim connect As String = ConfigurationManager.ConnectionStrings("FixedLineProvisioningConnectionString").ToString()
Using conn As New System.Data.SqlClient.SqlConnection(connect)
Using cmd As New System.Data.SqlClient.SqlCommand(query, conn)
cmd.Parameters.AddWithValue("", ID)
conn.Open()
ID = cmd.ExecuteScalar()
End Using
End Using
-
Aug 12th, 2009, 08:50 AM
#6
Re: Display @id=scope_identity()"
Code:
cmd.Parameters.AddWithValue("@nt_username", UserNameVar)
replace the UserNameVar with the DIM that contains the user name.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Aug 12th, 2009, 08:53 AM
#7
Re: Display @id=scope_identity()"
one sec..
you have the whole statement wrong
your new Statement:
Code:
Dim query As String = "INSERT INTO tblTotalComms_custinfo (nt_username) VALUES(@nt_username); SELECT SCOPE_IDENTITY()"
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Aug 12th, 2009, 09:05 AM
#8
Thread Starter
Addicted Member
Re: Display @id=scope_identity()"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|