Tom
This code will help you....
It gets a datareader then binds it to the dropdownlist using in quotes the
name of the columns for the datavaluefield and datatextfield.
Therefore you do not need to use
dr.GetValue(1).ToString();
instead use:
dr.DataValueField = "yourkeyfield - column name in the reader"
dr.DataTextField = "yourtextfield - column name in the reader"
Hope it helps
Kuv
------------
SqlConnection conn = new SqlConnection("database=northwind; server=(local);
user=sa; pwd=;");
conn.Open();
SqlDataReader dr;
SqlCommand cmd = new SqlCommand("Select * from
customers",conn);
dr = cmd.ExecuteReader();
dr1.DataSource = dr;
dr1.DataTextField = "contactname";
dr1.DataValueField = "customerid";
dr1.DataBind();
"TOm" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
How do I bind a SqlDataReader to a dropdownlist box?
I know how to construct a SqlDataReader dr and get the
values. Buit how do I bind it to a DropDownList box in C#?
This does not work
cboUsers.DataSource = dr;
cboUsers.Items.Clear();
cboUsers.DataTextField = dr.GetValue(1).ToString();
cboUsers.DataValueField = dr.GetValue(0).ToString();
cboUsers.DataBind();
Please advise.
Thanks
Tom