Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand your question correctly, this should do what you are seeking:</p> <p>Load your combo box with something like THIS (I can't test this right now, so I may have made some typo's or minor syntax errors):</p> <p><em>*</em>*UPDATED: Ok. That is the LAST time I try to answer a question "on the go" when I'm in a rush. My original code was rife with issues and stupid typos. My sincere apologies! The following code includes a very basic version of everything you are trying to do. You may need to adapt it to suit your needs.</p> <p>Some suggestions:</p> <p>A. Place your connection and command in the using blocks, as shown. </p> <p>B. Instead of hard-coding your connection string in your code, use the Properties.Settings designer in the Solutions Explorer (off to the left) and create a central reference for your connection string. Then reference it in code like shown. </p> <p>The following performs the basic functionality you are trying to achieve, and runs on my machine:</p> <pre><code>public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { button1.Click += new EventHandler(button1_Click); this.FillDropDownList(); } void button1_Click(object sender, EventArgs e) { this.SaveComboBoxContent(); } public void FillDropDownList() { string SQL = "SELECT id, name FROM info ORDER BY name"; DataTable dt = new DataTable(); // Set the connection string in the Solutions Explorer/Properties/Settings object (double-click) using (var cn = new SqlConnection(Properties.Settings.Default.MyConnectionString)) { using(var cmd = new SqlCommand(SQL, cn)) { cn.Open(); try { dt.Load(cmd.ExecuteReader()); } catch (SqlException e) { // Do some logging or something. MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString()); } } } // UPDATED - The .ValueMember and .DisplayMember properties // refer to the string name of the field (oops!): comboBox1.DataSource = dt; comboBox1.ValueMember = "id"; comboBox1.DisplayMember = "name"; } public void SaveComboBoxContent() { string SQL = "INSERT INTO info2 (name_id) VALUES (@name_id)"; using (var cn = new SqlConnection(Properties.Settings.Default.MyConnectionString)) { using(var cmd = new SqlCommand(SQL, cn)) { cmd.Parameters.AddWithValue("@name_id", comboBox1.SelectedValue); cn.Open(); try { cmd.ExecuteNonQuery(); } catch (SqlException e) { // Do some logging or something. MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString()); } } } } } </code></pre> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload