Reputation: 219
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
Upvotes: 2
Views: 7879
Reputation: 354
In my case, this problem was caused by accidentally calling InitializeComponent()
of the whole form twice, once before and once after having set the combo box's ValueMember
and DisplayMember
.
Upvotes: 0
Reputation: 1486
This if you Used the DisplayMember and ValueMember properties and still get Null Error
You must set the DataSource property of your Combobox control if you want to use the DisplayMember and ValueMember properties. Code Example
List<(int,string)> F = new List<(int,string)>();
DataTable d = ConnandDisConnMode.Program.DisConnectedMode();
for (int i = 0; i < d.Rows.Count; i++)
{
F.Add(((int)d.Rows[i]["id"], d.Rows[i]["Name"].ToString()));
}
comboBox1.DisplayMember = "Text";
comboBox1.ValueMember = "Value";
foreach (var item in F)
{
ComboboxItem i = new ComboboxItem();
i.Text = item.Item2;
i.Value = item.Item1;
comboBox1.Items.Add(i);
}
This would make the Combobox.SelectedValue Return NUll You have to change The Code to be like this
comboBox1.DisplayMember = "Text";
comboBox1.ValueMember = "Value";
List<ComboboxItem> z = new List<ComboboxItem>();
foreach (var item in F)
{
ComboboxItem i = new ComboboxItem();
i.Text = item.Item2;
i.Value = item.Item1;
z.Add(i);
//comboBox1.Items.Add(i);
}
comboBox1.DataSource = z;
Upvotes: 0
Reputation: 66439
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
.
Upvotes: 3