Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
//insert order status information
db.Execute(@"
    INSERT INTO OrderStatus
    (Price, Status, PricingTime, WordsNum, DeliveryTime)
    VALUES (@0, @1, @2, @3, @4)",
    null, App.strUnVisited, null, null, null);

//I dont have AuditNum value (identity column in OrderStatus)
//insert order details
db.Execute(@"
    INSERT INTO OrderDetails
    (FileName, Lang, Description, Time, UserId, Subject, Field, AuditNum)
    VALUES (@0, @1, @2, @3, @4, @5, @6, @7)",
    ConcatFileNames, LangSelect, Description, DateTime.Now, WebSecurity.CurrentUserId, Subject, Field, AuditNum );


I have a table 'OrderStatus' with identity column 'AuditNum'. The problem occurs when I wana do the second insert (in OrderDetails table). I need the AuditNum value that is automatically generated while the first inserting in order to give it to the second insert method as a (last) parameter. How can I do that (without concurrency problem)?
Posted
Updated 28-Apr-13 6:11am
v3

1 solution

Hello,

DatabaseClass in WebMatrix has a method named GetLastInsertId which Returns the identity column of the most recently inserted row. Use it to retrieve the AuditNum value and then issue the second insert. For more help see this link[^]

Regards,
 
Share this answer
 
Comments
cs101000 28-Apr-13 13:12pm    
Thank you!

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