Hi everyone,
Currently, I am using MVC on creating a project. Now I want to insert an Identity ID value into an INSERT statement.
Example:
string payment = @"INSERT INTO Payment(Payment_Method,Currency_Type,Total_Amount)
VALUES('{0}','{1}',{2})";
int pay = DBUtl.ExecSQL(payment, "Cash", currency,total);
So the above codes is a SQL statement that I used for INSERT values into the database which is the payment table and is successfully been run and inserted into the table.
Now, I trying to get the ID that has been inserted into the Payment Table for the next INSERT Statement for Order table. As there is a relationship between these 2 tables which is the payment_id is a foreign key in my Order Table.
So, I plan to retrieve the ID and then put it into my INSERT Statement for my Order table.
Please Help
Thank you
What I have tried:
So far, this is what I tried on getting the ID:
1) Include a SELECT statement while inserting:
int ord = DBUtl.ExecSQL(order, item.Cart_Service, item.Additional_Notes, item.Quantity, ("SELECT TOP 1 Payment_id FROM Payment ORDER BY Payment_id DESC"));
2) Assign a variable to get the value:
string pid = "SELECT TOP 1 Payment_id FROM Payment ORDER BY Payment_id DESC";
int paymentid = DBUtl.ExecSQL(pid);
And all of these are not working.