1

I want to do a normal INSERT statement from within ASP.NET, and I also want to return the ID that was used for the insertion ..

By the way, the database being used here is Oracle 11g ..

I know that within a stored procedure or a script, you can use something like this to return the ID inserted:

insert into mytable (...) values (...) returning id into v_id;

But since my call is being generated from within ASP.NET, I can't get the returning value.

Also, my due to misc. circumstances, I can't use Stored Procedures, or anything similar ..

Here is my current code:

System.Data.IDbConnection connection = this.CreateConnection();
connection.Open();
strIFRSCostID = taxDetails.Rows[0]["IFRSCOST_ID"].ToString();
strQuery = "INSERT INTO ....";
System.Data.IDbCommand cmd = this.CreateCommand(strQuery);
cmd.Connection = connection;
cmd.ExecuteNonQuery();
connection.Close();

Can I somehow insert AND get the inserted ID using the same command within ASP.NET ?

Ahmad
  • 12,886
  • 30
  • 93
  • 146
  • You don't specify how this ID is created. Is it a sequence? – OldProgrammer Jun 20 '13 at 13:39
  • please check this one http://stackoverflow.com/questions/5228780/how-to-get-last-inserted-id – Arif YILMAZ Jun 20 '13 at 13:49
  • You can try using the "insert into .. returning ..." clause. That will allow you to return a column value into a bound parameter. Not sure of the specifics regarding the parameters/etc, for C#. See this link - http://psoug.org/blogs/mohan/exploring-internal-params/returning-clause-in-oracle/ – OldProgrammer Jun 20 '13 at 13:50
  • 1
    Duplicate of [this question](http://stackoverflow.com/questions/12215454/return-an-sql-variables-value-from-within-an-oracle-sql-query-back-to-net-code/12216375#12216375). See my answer that uses RETURNING INTO clause with odp.net – tbone Jun 20 '13 at 14:00

0 Answers0