yasser alim

yasser alim

  • 2.1k
  • 17
  • 123

Insert into two tables with one table having an auto increment primary

Jun 15 2022 7:42 AM

I have 2 tables Station and Contract. Contract have a foreign key relationship with Station table IDStation auto increment . How i get IDStation number at same time i do insert statement.

cmd.CommandText = "Insert into Stations (CityID," +
                    "StartDate,Space,Owner)" +
                    "values (@CityID,@StartDate," +
                    "@Space,@Owner)" +
                    "Insert Contract (IDStation,ContractStart,ContractEnd) values (@IDStation,@ContractStart,@ContractEnd) ";

                cmd.Parameters.AddWithValue("@CityID", drpCity.Value);           
                cmd.Parameters.AddWithValue("@StartDate", txtStartDate.Value);               
                cmd.Parameters.AddWithValue("@Space", txtSpace.Text);     
                cmd.Parameters.AddWithValue("@Owner", txtOwner.Text);               
               // cmd.Parameters.AddWithValue("@IDStation",drp );
                cmd.Parameters.AddWithValue("@ContractStart", txtConStart.Value);
                cmd.Parameters.AddWithValue("@ContractEnd", txtConEnd.Value);
               
                cmd.ExecuteNonQuery();

Answers (2)