skip to Main Content

I’m trying to drag information from one table to create a category in a dropdown list. Next, I want to read the selected items into the "checkedlistbox" and save them in a table. Unfortunately, in the first case, I can display the category in the drop-down list, but then they are not transferred to the database. When trying to save elements with "checkedlistbox", only one is saved, I think that maybe need to use an array and not a string.

This is where I fill in the "combobox":

        private void getCategory()
        {
            conn.Open();
            DataTable dt = new DataTable();
            NpgsqlDataAdapter adap = new NpgsqlDataAdapter("SELECT * FROM workers", conn);
            adap.Fill(dt);
            cmbCoash.DataSource = dt;
            cmbCoash.DisplayMember = "name";
            cmbCoash.ValueMember = "name";
            conn.Close();
        }

Adding to the table:

                string Name = txtName.Text;
                string Type = txtType.Text;
                string Coash = cmbCoash.SelectedItem.ToString();
                string Days = "";

                  //checkedlistbox
                  foreach (object itemChecked in chDay.CheckedItems)
                  {
                      Days = itemChecked.ToString();
                  }

                var count = chDay.CheckedItems.Count;

                conn.Open();
                string query = $"INSERT INTO public.groupfit (name,type,trainingdays,сoach,total) VALUES('{Name}','{Type}','{Days}','{Coash}','{count}')";

                NpgsqlCommand nCommand = new NpgsqlCommand(query, conn);
                nCommand.ExecuteNonQuery();

                MessageBox.Show("Done.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                conn.Close();
                Select();
                cleaning();

This is what the added entry looks like
Do you really need to read "checkedlistbox" into an array for future storage?

2

Answers


  1. Chosen as BEST ANSWER

    I managed to fix the second problem that I described yesterday.

            private void getCategory()
        {
            string selectQuerry = "SELECT * FROM workers";
            NpgsqlCommand command = new NpgsqlCommand(selectQuerry, conn);
            NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(command);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmbCoash.DataSource = table;
            cmbCoash.ValueMember = "name";
        }
    

    And then this is how I get the variable to record it in the database:

    string Coash = cmbCoash.Text;
    

  2. I think you mean Days to be a comma delimited string.

    If that is the case, replace this

    foreach (object itemChecked in chDay.CheckedItems)
    {
       Days = itemChecked.ToString();
    }
    

    with this

    Days = string.Join(",", chDay.CheckedItems.Cast<object>().Select( x => x.ToString()) );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search