I want to return the value of Identity Key which in this case is an AutoNumber field in my table and place the value of that field of the newly added record into a textbox. Here's what I got so far, my insert works, but no return as of yet:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sql As New SqlCommand
'Dim myconn As Data.SqlClient.SqlConnection
Dim mycmd As Data.SqlClient.SqlCommand
Dim ResultReader As SqlDataReader
Dim result As String
Dim mySqlConnection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrin gs("MAS_XCMConnectionString").ConnectionString)
mySqlConnection.Open()
result = ""
ResultReader = Nothing
mycmd = New SqlCommand("SELECT * FROM CA_IT_STORYBOOK", mySqlConnection)
mycmd.CommandText = "INSERT INTO CA_IT_STORYBOOK(SalesOrderNumber, InvoiceNumber, StoryBook) VALUES (012,334,'NewTest3');Select @OutVar = SCOPE_IDENTITY()"
mycmd.ExecuteNonQuery()
' mycmd.ExecuteScalar()
If ResultReader.HasRows Then
result = result & vbCrLf & ResultReader.Item(0).ToString()
TextBox1.Text = result
End If
mySqlConnection.Close()
End Sub
6 1736
I think it's because "Select @OutVar = SCOPE_IDENTITY()" is just an assignment statement. If you really need that statement there, select @OutVar again to get its value. If not, you can select SCOPE_IDENTITY() directly instead.
You can do it like this: - mycmd.CommandText = "INSERT INTO CA_IT_STORYBOOK(SalesOrderNumber, InvoiceNumber, StoryBook) VALUES (012,334,'NewTest3');Select @@IDENTITY"
Visit this to know more about it.
Also, is your TextBox1 a multiline control? You are using a carriage return (vbCrLf), try to take that away if your textbox is not a multiline.
The main difference between @@SCOPE_IDENTITY and @@IDENTITY is
@@SCOPE_IDENTITY - Gets identity value for the last executed SQL statement with in the scope. Its a local variable
@@IDENTITY - Its a global variable. For the last executed statement if you have any trigger/function then after that stmt executed, it returns that stmts executed identity value.
Based on your requirement you use those varibles
The main difference between @@SCOPE_IDENTITY and @@IDENTITY is
@@SCOPE_IDENTITY - Gets identity value for the last executed SQL statement with in the scope. Its a local variable
@@IDENTITY - Its a global variable. For the last executed statement if you have any trigger/function then after that stmt executed, it returns that stmts executed identity value.
Based on your requirement you use those varibles
Thanks, how can I actually show the value of the Identity Id in a textbox?
ck9663 2,878
Recognized Expert Specialist
This would depend on your front-end tool that you used.
-- CK
This would depend on your front-end tool that you used.
-- CK
I just have a regular asp page with a button and a textbox. When I hit the button I want my record is being inserted into the table, but then I want to retrieve the Identity Id and show it in the textbox.
ck9663 2,878
Recognized Expert Specialist
First, make sure your stored proc/function is already returning what you want. On your front-end side, create a recordset object. Execute your module. It will return the value as an object of the recordset (column). You can store that value as value property of your textbox object inside your form.
-- CK
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Scrappy |
last post by:
I have an ASP front end on SQL 2000 database. I have a form that submits to
an insert query. The entry field is an "identity" and the primary key. I
have used scope_identity() to display the...
|
by: Lauren Quantrell |
last post by:
I'm using an Access2K front end on a SQL Server2K backend.
I use Scope_Identity() in a lot of stored procedures to pass the
newwly inserted record's unique ID to the next select statement in the...
|
by: Larry |
last post by:
I am seeing a problem with an ASP application, where I have 2 tables.
In the first table, the ASP inserts just 1 row and retrieves the
primary key of the new row using SCOPE_IDENTITY. It then uses...
|
by: Hardy Wang |
last post by:
Hi all,
I have the following codes, but SCOPE_IDENTITY() just returns NULL to
me. If I comment out SCOPE_IDENTITY() line and run @@IDENTITY line, it works
fine!! Since I have a trigger on the...
|
by: Mr Newbie |
last post by:
I was using this fine
sqlIDQuery = "SELECT SectionID FROM SECTIONS WHERE
(SectionID=SCOPE_IDENTITY());"
UNTIL. . . .
I added a parameter, now it fails, it doesent matter if I use the...
| |
by: Mr Newbie |
last post by:
The following code is comprises a function to create a section of a
documemt.
Each section is stored in a table. This successfully inserts a record in the
Sections table, but the
scope_identity()...
|
by: David Lozzi |
last post by:
Howdy,
ASP.Net 2.0 using VB on SQL 2005
This is a two fold issue.
I have a DetailsView control which users can insert or edit items. Editing
works great. Insert works great however I need...
|
by: Mike |
last post by:
Sql Server, Scope_Identity(), Ado.NET:
Which is better? Using an output parameter to return Scope_Identity through
ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the...
|
by: MrMancunian |
last post by:
Ok, I'm stuck on a design problem and I need some feedback how to go around it.
CASE:
Pathologists can request stains on certain tissues. It's possible to request more than one stain a time....
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |