Urgent!!!!!Storing checkboxlist item into database [Resolved]

Posted by Sina under ASP.NET on 6/18/2012 | Points: 10 | Views : 1739 | Status : [Member] | Replies : 2
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


Responses

Posted by: Jpchoudhari on: 6/18/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
make sure u r binding the check box list only once while page is loading first time
you should use this
  if (!IsPostBack)

{
BindCheckboxList()
}


Kind Regards,
Jay

Sina, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sina on: 6/18/2012 [Member] Starter | Points: 25

Up
0
Down
Yup thats true...but i found it little earlier..anyways thanks !!

@Sina

Sina, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response