Kate
Kate

Reputation: 372

Load data from SQL to combo and save selected value

i try to load values from sql database to combobox and after button click i want to save selected value of combobox to variable.

This is my code:

SqlConnection con = new SqlConnection(connectionstring);   
con.Open();
string strCmd = "select id,Prijmeni from Osoba";
SqlCommand cmd3 = new SqlCommand(strCmd, con);
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Prijmeni";
comboBox1.ValueMember = "id";
comboBox1.Enabled = true;
cmd3.ExecuteNonQuery();
con.Close();

private void Ulozit_Click(object sender, EventArgs e)
{
            //How i could save selected value of combobox to variable Value1 ???
}

Have you any ideas please?

Upvotes: 3

Views: 138

Answers (1)

Khan
Khan

Reputation: 342

You can save the selected value by:

var Value1 = comboBox1.SelectedValue;

Upvotes: 3

Related Questions