0

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();
        }
  }
1
  • First of all, read up on Database Normalization. By entering multiple values separated by a comma to the same field, you are violating the first normal form. en.wikipedia.org/wiki/Database_normalization Commented Jun 23, 2011 at 8:44

1 Answer 1

1
String selectedItem = string.Empty;
foreach (ListItem item in CheckBoxList1.Items)
{
    if (item.Selected)
    {
        **selectedItem += "," + item.Text;**
    }
}

if (selectedItem != string.Empty)
{
    selectedItem = selectedItem.Substring(1);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.