1

I try to get the id of the last inserted t ouristin the table as I put Selct SCOPE_IDENTITY; after the insert query.

The problem is that when I try to show the id of the inserted tourist in the label - just nothing happens. (only the insert query is executed);

string insertSQL = "INSERT INTO Tourist (Excursion_ID, Excursion_date_ID, Name_kir,Midname_kir, Lastname_kir)";



 insertSQL += "VALUES (@Excursion_ID, @Excursion_date_ID, @Name_kir,@Midname_kir, @Lastname_kir);Select SCOPE_IDENTITY()";

                string connectionString = "Data Source = localhost\\SQLExpress;Initial Catalog=excursion;Integrated Security=SSPI";
                SqlConnection con = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand(insertSQL, con);
                cmd.Parameters.AddWithValue("@Excursion_ID", Convert.ToInt32(mynew2));
                cmd.Parameters.AddWithValue("@Excursion_date_ID", Convert.ToInt32(mynewnewstring));
                cmd.Parameters.AddWithValue("@Name_kir",tx888.Text);
                cmd.Parameters.AddWithValue("@MidName_kir",tx888_1.Text);
                cmd.Parameters.AddWithValue("@LastName_kir",tx888_2.Text);



                int added = 0;
                try
                {
                    con.Open();
                    added = (int)cmd.ExecuteScalar();

                    if (added > 0)
                    {
                        lblproba.Text+=added.ToString();


                    }

                }
                catch (Exception ex)
                {
                    //lblResult.Text = ex.Message;


                }
Tania Marinova
  • 1,788
  • 8
  • 39
  • 67

0 Answers0