0

I'm going to Insert values to two table in Oracle.When i insert the record to first table and i need to get the Id of that table and it needs to enter the Second table.

Using my query It insert the values to first table.

  public bool AddNewNews(NEWS nws)
    {
        bool status = false;

        try
        {

                DatabaseProviderFactory factory = new DatabaseProviderFactory();
                Database db = factory.Create("CharikaConString");

                con.Open();

                string query = @"INSERT INTO NEWS_TBL(NewsID,NAME) 
                                 VALUES(newsid.nextval,:NAME)";


                cmd = db.GetSqlStringCommand(query);

                db.AddInParameter(cmd, "NAME", DbType.String, nws.NAME);

                db.ExecuteNonQuery(cmd);
// In the below commented lines are,I tried to insert second table but not success.
                  // String query2 = "Select newsid.currval";

                 //string query2 = @"INSERT INTO DTL_TBL(DtlId,desc) VALUES (newsid.currval,nws.Desc)";
                // cmd = db.GetSqlStringCommand(query);

                 //db.AddInParameter(cmd, "desc", DbType.String, nws.Desc);

                // db.ExecuteNonQuery(cmd);

        }
        catch (Exception ex)
        {

            throw;
        }
        finally
        {
            con.Close();
        }
        return status;
    }
TechGuy
  • 4,298
  • 15
  • 56
  • 87
  • Possible duplicate of [How to get last inserted id?](http://stackoverflow.com/questions/5228780/how-to-get-last-inserted-id) – Juan Carlos Oropeza Mar 03 '16 at 17:05
  • have you tried a simple google search, check this previous posting - http://stackoverflow.com/questions/1336911/how-to-get-the-generated-id-from-an-inserted-row-using-executescalar – MethodMan Mar 03 '16 at 17:07
  • @JuanCarlosOropeza thats for sql server,not oracle – TechGuy Mar 03 '16 at 17:26

1 Answers1

2

There are ways however you are using an oracle sequence, you could grab that value in a preliminary statement

select newsid.nextval from dual

and then use that as a regular parameter in your subsequent inserts?

Dominic Cotton
  • 789
  • 10
  • 34