Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load data from SQL to combo and save selected value

Tags:

c#

combobox

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?

like image 359
Kate Avatar asked Apr 24 '26 10:04

Kate


1 Answers

You can save the selected value by:

var Value1 = comboBox1.SelectedValue;
like image 185
Khan Avatar answered Apr 27 '26 10:04

Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!