Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have two tables

1)taskheader
2)taskdetails

taskheader columns

sno,taskname

taskdetails columns

tasknumber,assignee

in the taskheader sno column is auto increment and in taskdetails tasknumber column has
to be sno of taskheader.

both tables have to be populated with single click.

my problem is how to get the tasknumber form taskheader table
Posted
Comments
F-ES Sitecore 15-Jul-15 10:27am    
How can someone give you a solution when you haven't given any implementation details? Are you using ado.net? LinqtoSQL? Entity Framework? nHibernate? Something else? Are you looking to do this at a code level? At the database via a stored procedure?
PIEBALDconsult 15-Jul-15 10:28am    
I assume you are using a database. The actual technique will depend on which particular database system you are using. Please use Improve question to add such detail and context so we can better serve you.

Assuming you use a database, the best way is probably to write a stored procedure to do it - and enclose the whole operation in a transaction.
Then, within the transaction you can use @@IDENTITY[^] to fecth the value inserted in the first table, and insert it in the second.

But myself, I wouldn't use Identity values for this - I'd use a Guid value instead, and pass that through from the C# code.
 
Share this answer
 
There are several ways of getting the last inserted value for auto generated values such as identity or auto-increment. One simple is to requery the data using the natural key or to use function to get the latest value and so on.

However, I would prefer using OUTPUT clause[^]. This way you can do the INSERT and get the value easily in the same statement without extra roundtrips.
 
Share this answer
 

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