John Smith
John Smith

Reputation: 11

Use of Combobox in C# using SQL

First of all, thanks in advance for any answer. I'm a complete noob inn C#, and so far it's very interesting, yet complicated. My problem is this:

I connected an SQL Database to C#, I already display the "name" column in a combobox, but i need to use the "id" column. I guess it's more understandable with the code.

(I'm not going to put the code of the connection, because I think it is irrelevant).

private void Form1_Shown(object sender, EventArgs e)
{
    conexion.Open();
    MySqlCommand cm = new MySqlCommand("select * from tbestados", conexion);
    try
    {
        MySqlDataReader dr = cm.ExecuteReader();
        while (dr.Read())
        {
            comboBox1.Items.Add(dr["nombre"]);
        }
        dr.Close();
        dr.Dispose();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message,
                        Application.ProductName,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
    }
    conexion.Close();
}

private void button1_Click(object sender, EventArgs e)
{
    //When I press this button, i want to display the "id" of my table
    MessageBox.Show(Convert.ToString(comboBox1.ValueMember));
}

I'm gonna add some of MySql statements to go a little further.

insert into tbEstados (CveEstado, Nombre) values (1, "Aguascalientes");
insert into tbEstados (CveEstado, Nombre) values (2, "Baja California");
insert into tbEstados (CveEstado, Nombre) values (3, "Baja California Sur");

So, If I choose "Baja California Sur" from my ComboBox, I want my button to show "3".

Thanks!

Upvotes: 1

Views: 941

Answers (2)

Patrick D'Souza
Patrick D'Souza

Reputation: 3573

using a dataset as follows would be a more standard approach.

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT Nombre,CveEstado from tbestados", s);
                da.Fill(ds, "FillDropDown");

comboBox1.ItemsSource = ds.Tables["FillDropDown"].DefaultView;
comboBox1.DisplayMemberPath = "CveEstado";
comboBox1.SelectedValuePath = "Nombre";

Upvotes: 1

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

I hope your combobox code looks like :

<ComboBox  Name="comboBox1" Width="120" Height="22" ItemsSource="{Binding}"/>

and update your code:

private void Form1_Shown(object sender, EventArgs e)
{
    conexion.Open();       
    try
    {           
       MySqlDataAdapter da = new MySqlDataAdapter("select * from tbestados", conexion);
       DataSet ds = new DataSet();
       da.Fill(ds, "tbestados");
       comboBox1.ItemsSource = ds.Tables[0].DefaultView;
       comboBox1.DisplayMemberPath = ds.Tables[0].Columns["Nombre"].ToString();
       comboBox1.SelectedValuePath = ds.Tables[0].Columns["CveEstado"].ToString(); 
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message,
                        Application.ProductName,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
    }
conexion.Close();
}

and update your vbutton click as :

private void button1_Click(object sender, RoutedEventArgs e)
{     
MessageBox.Show("Selected Nombre="+comboBox1.Text+" and CveEstado="+  comboBox1.SelectedValue.ToString());
} 

Upvotes: 2

Related Questions