title: Storing Selected CheckedListBox Values in Database
body: <p>I have two tables in my database. Let's say table A and table B. table A values are put in checkedlistbox. The selected values in checkedlistbox then are put into table B. I tried to make a code however it wont work. Do you have any idea on how to make this problem work?</p>
  
  <p>thanks ahead guys. </p>
  
  <p>by the way im using c#.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using Npgsql;
  
  namespace WindowsFormsApplication1
  {
      public partial class Form8 : Form
      {
          public Form8()
          {
              InitializeComponent();
              this.Load += Form8_Load;
              button2.Click += button2_Click;
          }
  
          private void Form8_Load(object sender, EventArgs e)
          {
              string connstring = ("Server=localhost;Port=5432;User Id=postgres;Password=021393;Database=postgres;");
              NpgsqlConnection conn = new NpgsqlConnection(connstring);
              NpgsqlCommand cmd = new NpgsqlCommand("SELECT conname FROM condition", conn);
              cmd.CommandType = CommandType.Text;
              conn.Open();
              using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
              {
                  DataTable dt = new DataTable();
                  da.Fill(dt);
                  ((ListBox)checkedListBox1).DataSource = dt;
                  ((ListBox)checkedListBox1).DisplayMember = "conname";
                  ((ListBox)checkedListBox1).DisplayMember = "conid";
                  string[] condition = dt.Rows[0]["conname"].ToString().Split(',');
              }
  
          }
          private void button2_Click(object sender, EventArgs e)
          {
              string connstring = ("Server=localhost;Port=5432;User Id=postgres;Password=021393;Database=postgres;");
              NpgsqlConnection conn = new NpgsqlConnection(connstring);
              NpgsqlCommand cmd = new NpgsqlCommand("Insert into famhistory(famid) Values (@famid)", conn);
              conn.Open();
              cmd.Parameters.AddWithValue("@famid", checkedListBox1.Text);
              cmd.ExecuteNonQuery();
              MessageBox.Show("Data has been saved");
              conn.Close();
  
          }
  
      }
  }
  </code></pre>