upload combobox from mysql

0

I'm trying to load two values of a table into a combobox

What I need is that in my combobox the value nom_lugar is shown and that the id is assigned as a value to be able to enter it in the bd

        DateTime date = DateTime.Now;
        lbl_fecha.Text = date.ToShortDateString();
        try
        {

            MySqlConnection con = new MySqlConnection("server=localhost; 
            database=rier; Uid=rir; pwd=root; SslMode=none");
            string selectQuery = "select * from lugar";
            con.Open();
            MySqlCommand command = new MySqlCommand(selectQuery, con);

            MySqlDataReader reader = command.ExecuteReader();


            while (reader.Read()) {


                comboBox1.Items.Add(reader.GetString("id_lugar"));
                comboBox1.SelectedValue = "id_lugar";
            }


        }
        catch (Exception ex) {


            MessageBox.Show(ex.Message);
        }
    
asked by Wladimir Arnaldo Briceo Mirand 05.07.2018 в 23:41
source

1 answer

0

Try the following code

DateTime date = DateTime.Now;
            lbl_fecha.Text = date.ToShortDateString();
            try
            {

                MySqlConnection con = new MySqlConnection("server=localhost; 
                database=rier; Uid=rir; pwd=root; SslMode=none");
                string selectQuery = "select * from lugar";
                con.Open();
                MySqlCommand command = new MySqlCommand(selectQuery, con);

                MySqlDataAdapter mysqldt = new MySqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                mysqldt.Fill(dt);

                comboBox1.ValueMember = "id_lugar";
                comboBox1.DisplayMember = "nom_lugar";
                comboBox1.DataSource = dt;
            }
            catch (Exception ex) {


                MessageBox.Show(ex.Message);
            }
    
answered by 06.07.2018 / 00:17
source