0

I have brought the data as below into the combobox. In my "BRANDS" table, the first column is brand_id and the second column is brand_name. I'm getting the names in the combobox, but I need to get the id's when saving it to the database. How can I do it ?

void markaekle() {
    SqlCommand komut = new SqlCommand("Select * from MARKALAR", bgl.baglanti());
    SqlDataReader dr = komut.ExecuteReader();
    while (dr.Read())
    {
        comboBoxMarka.Properties.Items.Add(dr[1]);
    }
    bgl.baglanti().Close();
}

I need to save the id value to the database with a button like below:

private void BtnAnaGrupKaydet_Click(object sender, EventArgs e){
    SqlCommand komut = new SqlCommand("INSERT INTO ANA_GRUP (marka_id,anagrup_isim,create_date) values (@p1,@p2,@p3)", bgl.baglanti());
    komut.Parameters.AddWithValue("@p1", int.Parse(comboBoxMarka.Text));
    komut.Parameters.AddWithValue("@p2", txtAnaGrup.Text);
    komut.Parameters.AddWithValue("@p3", dateAnaGrup.DateTime);
    komut.ExecuteNonQuery();
    bgl.baglanti().Close();
    MessageBox.Show("Ana Grup Sisteme Eklendi", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Jimi
  • 29,621
  • 8
  • 43
  • 61

4 Answers4

0

You can do many implementations.

  1. Use Dataset and after executing your query, you can fill the dataset with the result. so you have all your data in your dataset. (define your database global) and after that when your button hits, you know which index of your combobox is selected. the index is the index of your dataset so you can access the id.

  2. The simplest way is that use a Dictionary with Id and Name. after that is the same above.

To sum up, you need to have a global variable which stores the Id and Name of your query result. this variable can be a Dictionary or a dataset or ....

0

In order to get selected value instead of text. you would bind combobox like following:

        List<DropDownKeyValue> list = new List<DropDownKeyValue>();

        List.Add(new DropDownKeyValue
        {
            ID = 1,
            Name = "Designation"
        });

        cmbSortBy.DisplayMember = "Name";
        cmbSortBy.ValueMember = "ID";
        cmbSortBy.DataSource = list;

to get selected ID you would use: cmbSortBy.SelectedValue object.

DropDownKeyValue class:

public class DropDownKeyValue
            {
                public int ID { get; set; }
                public string Name { get; set; }
            }
Zeeshanef
  • 639
  • 3
  • 14
  • 23
0

On the line below change values like.

komut.Parameters.AddWithValue("@p1",int.Parse(comboBoxMarka.SelectedValue.ToString()));
Quver
  • 1,408
  • 14
  • 21
uzain
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 20 '21 at 18:08
0

You need to bind your ComboBox to a DataSource. The easiest way for you to do this would be to amend your code along these lines:

SqlDataReader dr = komut.ExecuteReader();
var dt = new DataTable();
dt.Load(dr);
yourComboBox.DataSource = null;
yourComboBox.Items.Clear();
yourComboBox.DisplayMember = "brand_name";
yourComboBox.ValueMember = "brand_id";
yourComboBox.DataSource = dt;

You will now be able to access the id using yourComboBox.SelectedValue.

Jonathan Willcock
  • 5,012
  • 3
  • 20
  • 31