I’m trying to select two columns from my database, one to make it displaymember in my combobox and the other one to make it the valuemember
My code goes like:
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\administrator\documents\visual studio 2010\Projects\Clinic\Clinic\Clinc.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select ClinicName,ClinicCode from Clinic",con);
DataSet ds = new DataSet();
da.Fill(ds);
comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "ClinicName";
comboBox1.ValueMember = "ClinicCode";
It does fetch the data from database and display it well, but can’t set the valuemember.
When I try to print it to label to see it, the label displays “cliniccode”
What’s wrong ?
You should use the
comboBox1.SelectedValueproperty to get or set the value.