Click here to Skip to main content
16,018,394 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all ,

I am using typed datasets in asp.net,and i am inserting into one of the tables which have auto incremented primary key field.But i need to get the last value inserted in this field,which means the last row inserted in table.So how can i make it ?.I have already tried select identity but it did not work.So is there any other way ?

Thanks in advance.
Posted

I have already figured it out.So for all people that wants a solution for the same issue.This is what i did :

I added a stored procedure to the database and here it is :

SQL
ALTER PROCEDURE dbo.StoredProcedure1


    @Inventory_Title nvarchar(500)


AS
INSERT INTO Inventory
                         (Inventory_Title)
VALUES        (@Inventory_Title)
Select SCOPE_IDENTITY()
    /* SET NOCOUNT ON */
    RETURN


Then i added a query into the table adapter,that has the stored procuder as its source.Then i called the method from the code using c# and this is the code :

C#
DataSet1TableAdapters.InventoryTableAdapter da=new DataSet1TableAdapters.InventoryTableAdapter();

C#
int id = Convert.ToInt32( da.InsertWithReturn("Inv1"));


And by this way i got the last inserted id.
 
Share this answer
 
Comments
Tech Code Freak 26-Aug-11 0:01am    
Great that you have solved your problem!
My 5! + Bkmrkd
you can try :

ID_of_last_record+1
or
Insert and return Last record
 
Share this answer
 
Comments
Philippe Mori 25-Aug-11 19:23pm    
Might not works if there are concurrent access to the database...
In Visual Studio you can open your .xsd file and select the configure the table adapter. Select the "Advanced Options" button and select the "refresh data table" checkbox.
 
Share this answer
 
Comments
Philippe Mori 25-Aug-11 19:29pm    
I wasn't not even aware of that option. I never have any problem with SQL Express and dataset...

I began to have some problems when I want to add support for other databases (SQL CE and SQLite - dropped before conversion completed as it was a bit simpler to support CE) but I have to make many workarounds to make code works also for that database. It was not my choice though.

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