I want to insert each row in SQL into combobox, where EmployeeID will be combobox Value, and EmployeeFirstName EmployeeLastName will be text of combobox item. However this line<
Define a new class
public class EmpItem
{
public int empID;
public string empName;
}
While reading the DataReader create an instance of this class and add it to the combobox items collections. Do not forget to set the DisplayMember and ValueMember of the combobox
void comboboxrefresh()
{
comboBox1.DisplayMember = "empName";
comboBox1.ValueMember = "empID";
cnn.Open();
SqlCommand cmd = new SqlCommand("SELECT EmployeeID,EmployeeFirstName,EmployeeLastName FROM Employees", cnn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
EmpItem ei = new EmpItem() { empID=dr.GetInt32(0), empName = dr.GetString(1) + dr.GetString(2)};
comboBox1.Items.Add(ei);
}
}
cnn.Close();
}