0

Here is ehat I try to do

on button_click I read the values from the text boxes and insert them in them in the database. as tourist number for example maybe two or three tourists with ExecuteScalar; i get the ids of teh tourists which are inserted!

public void cmdInsert_OnClick(object sender, EventArgs e) {

        if (Page.IsValid)
        {
            string numtourist = (string)Session["tourist_number"];
            for (int i = 0; i < Int32.Parse(numtourist); i++)
            {
                TextBox tx888 = (TextBox)FindControl("txtNameK" + i.ToString());
                TextBox tx888_1 = (TextBox)FindControl("txtMidNameK" + i.ToString());
                TextBox tx888_2 = (TextBox)FindControl("txtLastNameK" + i.ToString());

                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 @@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;
                try
                {
                    con.Open();
                    added = (int)cmd.ExecuteScalar();


                    lblproba.Text = "";
                    Tourist.Add(added);
                    lblproba.Text += Tourist.Count();


                }
                catch (Exception ex)
                {
                    lblproba.Text += ex.Message;


                }
                finally
                {
                    con.Close();

                }
            }
            createReservation();
        }
    }

I call CreateReservationFunction AND i CREATE A NEW RESERVAION WITH THE ID OF THE USER WHO HAS MADE THE RESERVATION. wITH SELECT IDENTITY I TRY TO GET THE RESERVATION_ID of the reservation and here I get the exception "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack". So I wonder can this exception has something commn with the fact that in my solution exceptthe asp.net web projectI got library class in which I has .edmx file The entity framework model of my database and in my last form I don't use Ado,net but Entity framework

public void createReservation() {

        string insertSQL = "Insert INTO RESERVATIONS (User_ID) values (@User_ID)  SELECT @@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("@User_ID", 9);


        try
        {
            con.Open();
            string added = cmd.ExecuteScalar().ToString();
            createTouristReservation(added);


        }
        catch (Exception ex)
        {

            lblproba.Text+= ex.Message; 

        }




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

1 Answers1

0

Don't use @@IDENTITY but SCOPE_IDENTITY and add a semicolon between the insert and the select.

string insertSQL = @"INSERT INTO Tourist (Excursion_ID, Excursion_date_ID, Name_kir,Midname_kir, Lastname_kir)
                     VALUES (@Excursion_ID, @Excursion_date_ID, @Name_kir,@Midname_kir, @Lastname_kir) 
                    ;SELECT CAST(scope_identity() AS int)";
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939