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();
}