Get ID of selected value in drop down

I am trying to save data in my database, as part of that I am trying to get the ID (primary key) of my selected item in my drop down.

It keeps saving the same ID all the time. It only saves the first ID of any chosen item. How can I adjust my code so that it saves the different IDs based on what I have chosen in my drop down

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(
    WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("Select UserRoleID, RoleType from tb_UserRoles", con);
    DropDownList1.DataSource = cmd.ExecuteReader();
    DropDownList1.DataTextField = "RoleType";
    DropDownList1.DataValueField = "UserRoleID";
    DropDownList1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(
    WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("sp_UserInsertUpdate", con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    String ID = DropDownList1.SelectedValue;
    cmd.Parameters.AddWithValue("UserID", (hfUserID.Value == "" ? 0 : Convert.ToInt32(hfUserID.Value)));
    cmd.Parameters.AddWithValue("UserRoleID", ID);
    cmd.Parameters.AddWithValue("Username", TextBox1.Text);
    cmd.Parameters.AddWithValue("Password", TextBox2.Text);
    cmd.Parameters.AddWithValue("Email", TextBox3.Text);
    cmd.Parameters.AddWithValue("DateCreate", TextBox4.Text);
    cmd.ExecuteNonQuery();
    con.Close();
}

Upvotes: 3

Views: 1306

Answers (2)

I A Khan
I A Khan

Reputation: 8859

You should check !IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack) // Add this If condition
    {
        SqlConnection con = new SqlConnection(
        WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("Select UserRoleID, RoleType from tb_UserRoles", con);
        DropDownList1.DataSource = cmd.ExecuteReader();
        DropDownList1.DataTextField = "RoleType";
        DropDownList1.DataValueField = "UserRoleID";
        DropDownList1.DataBind();
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(
    WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("sp_UserInsertUpdate", con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    String ID = DropDownList1.SelectedValue;
    cmd.Parameters.AddWithValue("UserID", (hfUserID.Value == "" ? 0 : Convert.ToInt32(hfUserID.Value)));
    cmd.Parameters.AddWithValue("UserRoleID", ID);
    cmd.Parameters.AddWithValue("Username", TextBox1.Text);
    cmd.Parameters.AddWithValue("Password", TextBox2.Text);
    cmd.Parameters.AddWithValue("Email", TextBox3.Text);
    cmd.Parameters.AddWithValue("DateCreate", TextBox4.Text);
    cmd.ExecuteNonQuery();
    con.Close();
}

Upvotes: 2

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

This is because every time you click on button. click event is occurred but before that it executes the Page Load event and In page load event you have added the logic of binding drop down. it means whenever you click button then every time it first load the drop down list then comes to the click event. for this purpose you just need to add one if condition using IsPostBack.

Make the changes as below

protected void Page_Load(object sender, EventArgs e)
{
   if(!IsPostBack) //Added
   {
     SqlConnection con = new SqlConnection(
     WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
     con.Open();
     SqlCommand cmd = new SqlCommand("Select UserRoleID, RoleType from tb_UserRoles", con);
     DropDownList1.DataSource = cmd.ExecuteReader();
     DropDownList1.DataTextField = "RoleType";
     DropDownList1.DataValueField = "UserRoleID";
     DropDownList1.DataBind();
  }
}

Upvotes: 2

Related Questions