Click here to Skip to main content
11,707,167 members (59,023 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
i want insert record in multiple table but suppose i have two table one have primary key another have foreign key and insert record into this two table at one time.plz help me?
Posted 21-Mar-14 22:58pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The best way is to create a stored procedure, if you can - and use a transaction within that.
If not, then use a Transaction in your "normal" code.
But the procedure is the same in either case: write the Primary key table entry first, then write the Foreign key entry using the new data from the Primary key table. If either fails, you rollback the transaction, otherwise you commit it.

I can't give exact details on the code, because it will be different for a stored procedure, or VB or C#...but Google will help you with the parts anyway.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 141
1 Sergey Alexandrovich Kryukov 135
2 chainerlt 110
3 Maciej Los 80
4 ppolymorphe 79
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,512
2 CPallini 5,189
3 Maciej Los 4,766
4 Mika Wendelius 3,696


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 22 Mar 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100