Note that there are some explanatory texts on larger screens.

plurals
  1. POc# How to show name and insert values (name's ID ) from combobox in c#
    primarykey
    data
    text
    <p>Thank you guys in advance. I'am new user in c# Windows Forms. </p> <p>I have a table with id's and name</p> <pre> ID | Name --------------- 1 | Lion 2 | Tiger 3 | Crocodile </pre> <p>If I want to display from a table to combobox I did like this.</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.Windows.Forms; using System.Data.SqlClient; namespace Insert_update_delete_nr2 { public partial class Form1 : Form { SqlConnection con = new SqlConnection(@"CONNECTION_STRING"); SqlCommand cmd = new SqlCommand(); SqlDataReader dr; public Form1() { InitializeComponent(); } private void button1_Click_1(object sender, EventArgs e) { con.Open(); string query = "select * from info"; SqlCommand cmd = new SqlCommand(query, con); cmd.CommandType = CommandType.Text; dr = cmd.ExecuteReader(); while (dr.Read())//while true { comboBox1.Items.Add(dr[0].ToString());//loading values into combo } cmd.CommandText = "insert into info3 (name, name_id) values ('"+textBox1.Text+"', '" + comboBox1.Items.Add(dr[0].ToString()) + "')"; cmd.ExecuteNonQuery(); cmd.Clone(); con.Close(); } private void loadlist() { listBox1.Items.Clear(); listBox2.Items.Clear(); listBox3.Items.Clear(); con.Open(); cmd.CommandText = "select * from info3"; dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { listBox1.Items.Add(dr[0].ToString()); listBox2.Items.Add(dr[1]).ToString(); listBox3.Items.Add(dr[3].ToString()); } } con.Close(); } private void Form1_Load(object sender, EventArgs e) { // con.Open(); FillDropDownList(string SQL, ComboBox comboBox1);// This giving me error. // How should I call this FillDropDownlist function? The parameters which are they? cmd.Connection = con; listBox3.Visible = false; loadlist(); } } } </code></pre> <p><br> And it's trying inserting what is showing in combobox which it's name, not id's. <br></p> <p>in PHP it would be like the following:<br></p> <pre><code>$sql = " SELECT * FROM info "; $res = mysql_query($sql); while ($row = mysql_fetch_array($res)) { print '&lt;option value="'.$row['id'].'"&gt;'.$row['name'].'&lt;/option&gt;'; } </code></pre> <p>That would insert id's and showing names. But How should I do in c#? Thank you again for your time!</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.
 

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