0

I'm new to this and I have a checkbox list with multiple values, And I cant figure out what the attribute is to send the values to a field in the SQL database if multiple are selected.

The SQL database accepts the data the way i have it set up, but it only receives the first selection which makes sense with SelectedValue, but i am unsure what attribute i can use to send the multiple values.

<asp:CheckBoxList ID="dentalcare" runat="server" class="checkbox">
        <asp:ListItem>Emergency Only</asp:ListItem>
        <asp:ListItem>Routine Care</asp:ListItem>
        <asp:ListItem>Needs Early Dental Work</asp:ListItem>
        <asp:ListItem>Infrequent Visits</asp:ListItem>
        <asp:ListItem>None</asp:ListItem>      
    </asp:CheckBoxList>




protected MedicalSave collectFormData()
    {
        MedicalSave medicalSave = new MedicalSave();
        medicalSave.Dentalcare = dentalcare.SelectedValue; 
        return medicalSave;
    }




public class MedicalSave
 {

    private string dentalcare;

    public string Dentalcare
    {
        get
        {
            return dentalcare;
        }
        set
        {
            dentalcare = value;
        }
    }
}




private static string GetConnectionString()
    {
        return 
 ConfigurationManager.ConnectionStrings 
["MySQLConnectionString"].ConnectionString;
    }

    public void SaveMedicalSave(Objects.MedicalSave MedicalSave)
    {
        var cmd = new SqlCommand("SaveMedicalDental", new 
SqlConnection(GetConnectionString()));
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Connection.Open();
        cmd.Parameters.AddWithValue("@dentalcare", 
MedicalSave.Dentalcare);
      cmd.ExecuteNonQuery();
    }
  • Possible duplicate: https://stackoverflow.com/questions/18924147/how-to-get-values-of-selected-items-in-checkboxlist-with-foreach-in-asp-net-c – Dhaust Apr 03 '19 at 01:25
  • I used the code found in the marked duplicate post, and i got these to work: private ListItemCollection dentalcare; List selected = dentalcare.Items.Cast() .Where(li => li.Selected) .ToList(); MedicalSave medicalSave = new MedicalSave(); medicalSave.Studentidmed = studentidmed.SelectedValue; medicalSave.Dentalcare = dentalcare.Items; but im still having trouble with this part: cmd.Parameters.AddWithValue("@dentalcare", MedicalSave.Dentalcare); – Greg S. Apr 03 '19 at 15:14

0 Answers0