The Issue:
I populate a checkbox list from a SQL table:
public static List<string> populateCheckBoxes(string type)
{
List<string> items = new List<string>();
items.Add("");
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand sqlcmd;
switch (type)
{
case "referralorsignposting":
sqlcmd = new SqlCommand("SELECT * FROM SWApp_List_Equipment WHERE type = 'Referral or Signposting' ORDER BY order_no, name", conn);
break;
case "actionstaken":
sqlcmd = new SqlCommand("SELECT * FROM SWApp_List_Equipment WHERE type = 'Actions Taken' ORDER BY order_no, name", conn);
break;
default:
sqlcmd = new SqlCommand("SELECT * FROM SWApp_List_Equipment", conn);
break;
}
SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
items.Add(dr["name"].ToString());
CheckboxIDRecord = dr["id"].ToString();
//items.Add(dr["VisitTime"] + " " + dr["PropPostcode"]);
}
return items;
}
I have iterated through each value that is selected in the "checkboxlist" and this inserts each value selected:
foreach (var item in saw.actionsTakenCheckBoxList)
{ //ADD ACTIONS AND REFERRAL
SqlCommand add = new SqlCommand("INSERT INTO SWApp_CheckboxAnswers (SW_ID, Checkbox_ID, Checkbox_Section, Checkbox_Type, Checkbox_Answer) VALUES(@SW_ID,@Checkbox_ID,@Checkbox_Section,@Checkbox_Type,@Checkbox_Answer) ");
add.CommandType = CommandType.Text;
add.Connection = sqlcon;
add.Parameters.AddWithValue("@SW_ID", "");
add.Parameters.AddWithValue("@Checkbox_ID", "");
add.Parameters.AddWithValue("@Checkbox_Section", "");
add.Parameters.AddWithValue("@Checkbox_Type", "");
add.Parameters.AddWithValue("@Checkbox_Answer", "");
add.Parameters["@SW_ID"].Value = saw.EntryID.ToString();
add.Parameters["@Checkbox_ID"].Value = CheckboxIDRecord.ToString();
add.Parameters["@Checkbox_Section"].Value = "SmokeDetectionReferral";
add.Parameters["@Checkbox_Type"].Value = "";
add.Parameters["@Checkbox_Answer"].Value = item.ToString();
sqlcon.Open();
add.ExecuteNonQuery();
sqlcon.Close();
}
As you can see what i have currently tried only inputs the ID for the first value selected in the Checkboxlist.
The Aim:
The aim is to have insert the value of the checkbox which is "name" and also the "id" of the each item.
Research:
I tried following this article to put the items into an array but ended up with a 'Array was out of bounds of the index' which led me to the second article.
Pass items from checkboxlist to SQL Server table
Index was out of bounds of array? c# forms
I would appreciate any guidance with this. Thanks.
Aucun commentaire:
Enregistrer un commentaire