C# .NET - how to display listbox item's value in a label

Asked By Angie on 20-Sep-10 03:36 AM

i have a listbox which displays all employee names, as i click employee's name i want its EmpId to be displayed on a label.

EmpId is the primary key here.

SqlCommand sqlComm = new SqlCommand("SELECT EmpID,FirstName FROM Employees", sqlConn);

        myReader = sqlComm.ExecuteReader();

        if (myReader != null)

          while (myReader.Read())




now to display EmpID in the label

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        this.label1.Text = listBox1.SelectedValue.ToString();

but this code doesn't show anything in label....
Kirtan Patel replied to Angie on 20-Sep-10 03:46 AM

you need to Write Code like below to Get EmpID from database in ListBox SelectedIndex Change Event

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
         this.label1.Text = listBox1.SelectedValue.ToString(); 
         SqlCommand sqlComm = new SqlCommand("SELECT EmpID FROM Employees Where FirstName=@FirstName", sqlConn);
         sqlComm.Parameters.AddWithValue("@FirstName", listBox1.SelectedValue.ToString());
         this.label1.Text  = sqlComm.ExecuteScalar().ToString();

Sagar P replied to Angie on 20-Sep-10 04:04 AM
You have to specify value member also while adding items in listbox... as its null in your code its not getting any value at the time of listBox1.SelectedValue.ToString()...

So use this code to bind data to list box... then it will give you proper empId as the time of listBox1.SelectedValue.ToString();

SqlCommand sqlComm = new SqlCommand("SELECT EmpID,FirstName FROM Employees", sqlConn);
SqlDataReader myReader = sqlComm.ExecuteReader();
if (myReader != null)
  listBox1.DataSource = myReader;
  listBox1.DisplayMember = "FirstName";
  listBox1.ValueMember = "EmpID";
Sasha Kotlo replied to Angie on 20-Sep-10 04:06 AM
Your code is wrong in the beginning which is why you dont see any value. Everything is fine but this part:

 if (myReader != null)

      while (myReader.Read())



so change it to this:

 if (myReader != null)

      while (myReader.Read())
    ListItem item = new ListItem();
    item.Text = myReader["FirstName"].ToString();
    item.Value = myReader["EmpID"].ToString();


That's it.

Anand Malli replied to Angie on 20-Sep-10 04:39 AM
Hi Angie

Everything is ok but why you are not getting is because you never set it...so to get selectedvalue first you need to set it..so as sasha has already provided you the solution..i am not writting it again over here, but set both Text to be displayed and its value while looping through....

Super Man replied to Angie on 20-Sep-10 06:19 AM
You can make use of this code:

            SqlConnection sqlConn = new SqlConnection("connecion string here");

    SqlCommand sqlComm = new SqlCommand("SELECT EmpID,FirstName FROM Employees", sqlConn);


    SqlDataAdapter da = new SqlDataAdapter(sqlComm);

    DataTable dt = new DataTable();



    listBox1.DataSource = dt;

    listBox1.DisplayMember = "FirstName";

    listBox1.ValueMember = "EmpID";


 Now your listbox selectedindexchanged event works fine for you.


Goniey N (Mr. G) replied to Angie on 20-Sep-10 09:03 AM
-- Use Below Code :

01.private void button2_Click(object sender, EventArgs e)
03.      SqlConnection Conn = new SqlConnection("<<Your Connection String>>");
04.      try
05.      {
06.        Conn.Open();
07.        SqlDataReader dr;
08.        string str = "SELECT EmpName FROM Employee";
09.        SqlCommand cmd = new SqlCommand(str, Conn);
10.        dr = cmd.ExecuteReader();
11.        while (dr.Read())
12.        {
13.      //Add Employee Names in Listbox...
14.          listBox1.Items.Add(dr.GetValue(0).ToString());
15.        }
16.        Conn.Close();
17.      }
18.      catch (Exception ex)
19.      {
20.        Conn.Close();
21.        MessageBox.Show(ex.ToString());
22.      }
26.private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
28.      SqlConnection Conn = new SqlConnection("<<Your Connection String>>");
29.      try
30.      {
31.        Conn.Open();
32.        SqlDataReader dr;
33.        string str = "SELECT EmpId FROM Employee Where EmpName='" + listBox1.SelectedItem.ToString() + "'";
34.        SqlCommand cmd = new OleDbCommand(str, Conn);
35.        dr = cmd.ExecuteReader();
36.        while (dr.Read())
37.        {
38.      //Display Employee Id Depending On The Employee Name In Label...
39.          lblEmpId.Text=dr.GetValue(0).ToString();
40.        }
41.        Conn.Close();
42.      }
43.      catch (Exception ex)
44.      {
45.        Conn.Close();
46.        MessageBox.Show(ex.ToString());
47.      }

-- For More Clear View :

-- See Table Data..

-- When User Click On The Button It Will First Of All Bind The Names Of Empolyee To Listbox. Then Whne User Select The Name Of Employee Immediately It Will Display Employee Id in Label...

-- It Will Work 100%...
-- Hope This Will Help You...