Click here to Skip to main content
15,885,949 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
First I inserted a value:

XML
QUERY( Insert into Table values ("TRY"))


Then I wanted to put the value of the last inserted id to a textbox. Since I do not know how to bind in textbox . I used gridview first.

XML
<pre lang="xml">QUERY(Select last_insert_id)
                   GRV_Dat7.DataSource = ds
                   GRV_Dat7.DataBind()
                   TextBox_IDNew1.Text = GRV_Dat7.Rows(0).Cells(0).Text</pre>



But this didnt work. Why? Can anyone help me? thanks.
Posted
Comments
[no name] 14-Oct-14 1:10am    
You can check with one query select Max(ID) from TableName

by using @@IDENTITY in sql server u will get last inserted id and using output parameters u can retrive that value in to ur code
 
Share this answer
 
Comments
SRS(The Coder) 14-Oct-14 0:14am    
@@IDENTITY works across all scopes but i think here we need to use SCOPE_IDENTITY() as this is specific to particular scope we are inserted the record.

If we will go for using @@IDENTITY it will return the last inserted ID for any other insertion if made after yours within the same session.

Please look into this Article for further details :-
http://www.codeproject.com/Articles/103610/Difference-between-IDENTITY-SCOPE-IDENTITY-IDENT-C
Namith Krishnan E 14-Oct-14 0:21am    
yes....u r right,iam assuming he is inserting into one table
Hi NekoNao,

In ASP.Net to get the last inserted id from SQL Server you can go like this :-

1) Write a procedure to insert the records by passing parameter as all the values you want to insert to a specific table. Also declare an extra output parameter to get the last inserted record id to the table in that scope.
Ex :-
SQL
CREATE PROCEDURE [dbo].[SP_InsertTestData]
(
      @Value1                VARCHAR(20)
    , @Value2                VARCHAR(MAX)
    , @Value3                VARCHAR(50) = ''
    , @LastInsertedRecordID  INT OUTPUT
)
AS
BEGIN
**********
END


2) You can get the last inserted record id using SCOPE_IDENTITY() method in SQL Server using just after the insertion operation then assign this value to the particular output parameter.
Ex:-
SQL
--Inside the procedure body
--Below statement goes just after insert statement
SET @LastInsertedRecordID = SCOPE_IDENTITY()


3) Now to get this ID in ADO.Net code and to set this value to a particular Label or TextBox is being explained here which might be further helpful for you.

Click Here[^]


Hope this definitely of help to you.
 
Share this answer
 
v3
simple try this,
select MAX(your_identityId) from table_name

bind this value to textbox which is in ur gridview.
 
Share this answer
 
v2
Comments
SRS(The Coder) 14-Oct-14 0:15am    
This one is also helpful but need to know and use carefully.
Bhavikkumar Chaudhari 14-Oct-14 0:21am    
Hi Gopu Raj,

You are right but there might be issue of concurrency.
If two users will do entry in same table simultaneously then issue will happen.

Better way is to use @@IDENTITY, it will return last identity value from table.
Rajesh waran 14-Oct-14 0:34am    
s i agree,but i tried this query with my table simultaneously,it works,i can understand sometimes there will be some issues.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900