Valerie Meunier

Valerie Meunier

  • 947
  • 693
  • 71.3k

inserted value is 0

Jun 25 2022 9:39 AM

Hi

i'm trying to insert a int value (id) into a table of sql server. That value is the part before the space of the selectedvalue of a dropdownlist. Because id is defined as internal, i thought it would keep its value in all events, but obviously doesn't. Why and how to fix that (possible with session variable)? I put the property CausesValidation="false" of the button but still same problem.

Thanks

V

internal int id;

 protected void dd2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sel = dd2.SelectedValue;
            int pos = sel.IndexOf(" ");
            id = Int32.Parse(sel.Substring(0, pos));
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label2.Text = id.ToString();// this shows 0
            mConnection.Open();
            sql = "insert into number (idll) values (@idll)";
            comd = new SqlCommand(sql, mConnection);
            comd.Parameters.AddWithValue("@idll", Convert.ToInt32(id));
            comd.ExecuteNonQuery();
            mConnection.Close();
        }


Answers (4)