I have an asp.net form with C# code behind and on the form I have a CheckBoxList. Ideally users can use this CheckBoxList to select multiple items that apply to them and have all the selections inserted into a single column in an SQL database, with each selection separated by commas. Below is my current code for the CheckBoxList INSERT but when I query the SQL table on SSMS, the CheckBoxList column shows simply as 'NULL'.
Pyramid is the name of the database table. CheckBoxListFruits is the name of the CheckBoxList control, Favorite Fruits is the name of the column I'm trying to insert the information into.
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "INSERT INTO Pyramid set IsSelected = @IsSelected" + " WHERE Favorite Fruits=@Fruits";
foreach (ListItem item in CheckBoxListFruits.Items)
{
cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
cmd.Parameters.AddWithValue("@Fruits", item.Value);
}
}
What is wrong with my code and how can I fix it?
null? You don't need+in yourCommandTextas well.