Click here to Skip to main content
15,887,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public void cmbdisp()
{
    SqlConnection con = new SqlConnection(ConnectionString);
    cmboSelEmp.Items.Clear();
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd = con.CreateCommand();
    cmd.CommandText = "Select * from add_employee";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    foreach (DataRow dr in dt.Rows)
    {
        cmboSelEmp.Items.Add(dr["name"].ToString());
        cmboSelEmp.SelectedValuePath = (dr["name"].ToString());
        cmboSelEmp.SelectedValuePath = (dr["eid"].ToString());
    }

}


What I have tried:

public void cmbdisp()
{
    SqlConnection con = new SqlConnection(ConnectionString);
    cmboSelEmp.Items.Clear();
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd = con.CreateCommand();
    cmd.CommandText = "Select * from add_employee";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    foreach (DataRow dr in dt.Rows)
    {
        cmboSelEmp.Items.Add(dr["name"].ToString());
        cmboSelEmp.SelectedValuePath = (dr["name"].ToString());
        cmboSelEmp.SelectedValuePath = (dr["eid"].ToString());
    }

}
Posted
Updated 1-Sep-22 21:59pm

1 solution

The trouble with loops is, when you assign a value to a single variable inside one after the loop you only have a single value in it - the last one:
C#
foreach (int x in Enumerable.Range(0, 9))
   {
   lastvalue = x;
   }

So when your do the same thing with a datatable, the only result you get is always the last element in the table ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900