I have a little problem regarding my ASP.net project.I have register.aspx, login.aspx and default.aspx pages inside of my ASP.net project.
By the way, entering those informations which wanted from those users into the textboxes inside of the register.aspx page and after clicked the button, the information is writing into the database. In sum, my project is that. My question is - I will give multiple-choice with checkboxlist and I will write to the table inside of the database those items which choose in the checkboxlist and I want to write by putting a comma between of each item to table inside of the database.
I tried a code as below. I'm not getting the error. But it's coming as NULL the field (I'm using a column called hobby) inside of table. And it was record the same record twice.
String selectedItem = string.Empty;
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
string.Format(selectedItem, ",", item.Text);
}
}
if (selectedItem != string.Empty)
{
selectedItem = selectedItem.Substring(1);
}
Or, when I try to run a code as below,it just takes the first selected.
for(int i=0;i<CheckBoxList1.Items.Count;i++)
{
if (CheckBoxList1.Items[i].Selected == true)
{
cmd.Parameters.Add("@Hobies", SqlDbType.VarChar).Value =
CheckBoxList1.Items[i].Text.ToString();
cmd.ExecuteNonQuery();
}
}