Search code examples
c#ms-accesschecklistbox

Get checked Items into MS Access database


I´m using C# Windows forms application and i got 5 items in a checklistbox. Now I have to save these items into a MS Access database table.

Does anyone knows how i should do it? (Sprachen = checkedlistbox)

private void button1_Click_1(object sender, EventArgs e)
    {
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "INSERT INTO Employee (Vorname, Nachname, Wohnort, Geburtstag, Abteilung, Nummer, MKZ, Führerschein, Sprachen) values('" + Vorname.Text + "','" + Nachname.Text + "','" + Wohnort.Text + "','" + Geburtstag.Text + "','" + Abteilung.Text + "','" + Mitarbeiter.Text + "','" + MKZ.Text + "'," + Führerschein.Checked.ToString() + "," + Sprachen.SelectedItems + ")";

            command.ExecuteNonQuery();
            MessageBox.Show("Daten gespeichert!");
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex);
        }

everytime i tried it with toString(), it gives me an error: For at least one required parameter no value was specified.

I can only count how many items i´ve checked in the checklistbox.

Anyone got an solution?


Solution

  • Sprachen.SelectedItems returns list of selected items. If you are trying to insert the items which are checked in checkedlistbox then you have to loop for checklistbox.CheckedItems. Instead of "Sprachen.SelectedItems" pass checklistbox.CheckedItems[0].ToString() as value and check the code. Note: 0 is hard coded.. later you can loop it

    String type value must be passed within single quote. Check out last two fields.