Search code examples
c#combobox

C# ComboBox Selected Value returns Null, Item and Value both from Database


I added Employee LastName and First name to a combobox which is working, Eg. LastName, FirstName.. The value should be set as EmpID so that when I'm selecting an Item, EmpId will be return as value

conn.Open(); 
using (SqlCommand cmd = new SqlCommand("Select (LastName + ', ' + FirstName) AS Employee,EmpID from Employee ", conn))
using (SqlDataReader rdr = cmd.ExecuteReader())
{
    while (rdr.Read())
    {
        ListItem ComboItem = new ListItem();
        ComboItem.Text = rdr["Employee"].ToString();
        //ComboItem.Value = rdr["EmpID"].ToString();

        ComboItem.Value = rdr["EmpID"].ToString() ;
        GrpEmpCBox.Items.Add(ComboItem);  
        //GrpEmpCBox.Items.Add(rdr["Employee"].ToString());
    }
}

But when I'm getting the value of it returns null.

private void btnRunReport_Click(object sender, EventArgs e)
{
    EmployeeTimecardReport rptEmpTimecard = new EmployeeTimecardReport();
    rptEmpTimecard.Employee = GrpEmpCBox.SelectedValue.ToString(); //<--- this returns null
}

is this possible? I'm trying not to use databinding

my combobox properties:

DataSource: none

DisplayMember: none

Value Member: none

DropDownStyle: DropDownList


Solution

  • Use the DisplayMember and ValueMember properties instead.

    There are many ways you could implement this... here's one:

    comboBox1.ValueMember = "Item1";     // the value of the selected item
    comboBox1.DisplayMember = "Item2";   // the field to display to the user
    
    var emps = new List<Tuple<int, string>>();
    
    while (rdr.Read())
        emps.Add(Tuple.Create(Convert.ToInt32(rdr["EmpID"]), rdr["Employee"].ToString()));
    
    comboBox1.DataSource = emps;
    

    Then to display the value of the selected item:

    var selectedValue = comboBox1.SelectedValue.ToString();
    

    I'll add a second example to demonstrate what those properties do.

    Say you have an Employee class you created.

    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime HireDate { get; set; }
    }
    

    And then you pull back a bunch of data from the Employee table in the database...

    var emps = new List<Employee>();
    
    while (rdr.Read())
        emps.Add(new Employee(ID = Convert.ToInt32(rdr["EmpID"]),
                              Name = rdr["Employee"].ToString(),
                              Age = Convert.ToInt32(rdr["Age"]),
                              HireDate = Convert.ToDateTime(rdr["Hired"])));
    
    comboBox1.DataSource = emps;
    

    You can pick two properties from your class to (1) display to the user (DisplayMember) and (2) act as the underlying value (ValueMember).

    comboBox1.ValueMember = "ID";
    comboBox1.DisplayMember = "Name";
    

    Now you can get those values using comboBox1.SelectedValue and comboBox1.Text, respectively. Or you can just get the entire Employee record, which is stored in comboBox1.SelectedItem.