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();
Do you really need to read "checkedlistbox" into an array for future storage?
2
Answers
I managed to fix the second problem that I described yesterday.
And then this is how I get the variable to record it in the database:
I think you mean Days to be a comma delimited string.
If that is the case, replace this
with this