First off you need to add the Databind code to the Page_Load section of the
code, not the SelectedIndex Changed section.
Also, the biggest mistake people make when binding data to a dropdown is
that they rebind the data on every Page_Load. So be sure that in your
page_load section of the code you wrap the binding inside an if
(!Page.IsPostBa ck) block.
So it should look like this:
private void DropDownList1_S electedIndexCha nged(object sender,
System.EventArg s e)
{
// Do something with the changed value
}
private void Page_Load(objec t sender, System.EventArg s e)
{
if ( !Page.IsPostBac k )
{
OleDbConnection conn = new
OleDbConnection ("DataSource=nt drp001.world;
integrated security =true;"+ "initial catalog = Request");
conn.Open();
OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from
Request",
conn);
OleDbDataReader dreader = cmd.ExecuteRead er();
DropDownList1.D ataSource = dreader;
DropDownList1.D ataValueField = "cuid";
DropDownList1.D ataTextField = "cuid";
DropDownList1.S electedIndex = 0;
DropDownList1.D ataBind();
dreader.Close() ;
conn.Close();
}
}
"Brian Conway" <Br**********@q west.com> wrote in message
news:ek******** *****@tk2msftng p13.phx.gbl...
I need some help on binding a datareader to a dropdown box. I have
included the code for the dropdown below. It builds with no errors, but returns no
results. Any help would be appreciated.
private void DropDownList1_S electedIndexCha nged(object sender,
System.EventArg s e)
{
OleDbConnection conn = new OleDbConnection ("DataSource=nt drp001.world;
integrated security =true;"+ "initial catalog = Request");
conn.Open();
OleDbCommand cmd = new OleDbCommand("S ELECT distinct(cuid) from Request",
conn);
OleDbDataReader dreader = cmd.ExecuteRead er();
DropDownList1.D ataSource = dreader;
DropDownList1.D ataValueField = "cuid";
DropDownList1.D ataTextField = "cuid";
DropDownList1.S electedIndex = 0;
DropDownList1.D ataBind();
dreader.Close() ;
conn.Close();
}