I have a checkboxlist whose items are databound. The data is loaded into checkboxlist but the problem is even if i select an item it shows selected 'false'. Can you pl suggest the correct coding...plzz..am going mad....need help!! My code is below: chkCat is my checkboxlist.
protected void btnSaveChange_Click(object sender, EventArgs e)
{
string user = "";
user = Session["UserId"].ToString();
SqlConnection connn = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.Connection = con;
cmd.CommandText = "insert into UserCategory(UserId, CatId) values (@user,@cat)";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@user", user);
for (int i = 0; i < chkCat.Items.Count ; i++)
//foreach (ListItem li in chkCat.Items)
{
//String str = "";
if (chkCat.Items[i].Selected)
{
//str = chkCat.Items[i].Text;
cmd.Parameters.AddWithValue("@cat", chkCat.SelectedItem);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
@Sina