Copying bind Dropdownlist

C

Carlos

I have a dropdown list that binds data from a data reader bo I have a second
dropdown list in my wen form which I want to have same data, how can I copy
the one is bind to a secopnd one.. see my code below it dos not work..

Thanks

try

{

SqlConnection MyConnection = new SqlConnection();
MyConnection.ConnectionString = "user id=ScaleClient; password=
XXXAA;Initial Catalog=swatime;Data Source=Carbon";
MConnection.Open();



if(!IsPostBack)
{
SqlCommand MyCommand;
SqlDataReader MyReader;
String MySQL;
MySQL = "select * from clients";
MyCommand = new SqlCommand(MySQL,MyConnection);
MyReader = MyCommand.ExecuteReader();

//Fill Dropdownlist

FinOwnerList.DataSource = MyReader;
FinOwnerList.DataValueField = "ClientName";
FinOwnerList.DataBind();


// set Owner from Clients
PrjOwner.DataSource = MyReader;
PrjOwner.DataValueField = "ClientName";
PrjOwner.DataBind();
}
}
 
S

Steven Cheng[MSFT]

Hi Carlos,

Yes, the second DropDownList is empty because the DataReader is forward
only and after bind with the first DropDownList, it's cursor point to the
end of the ResultSet and won't be able to bind with any other control
again. If you do need to populate the second dropdownlist by resuing the
datas binding to the first DropDownList(without requery the db to fill the
datareader), you can consider bind the second dropdownlist with the First
DropDownList's Items Collection. For exampLe:

if(!IsPostBack)
{
conn.Open();
SqlDataReader dr = comm.ExecuteReader();

this.lstOne.DataSource = dr;
this.lstOne.DataTextField = "fname";
this.lstOne.DataValueField = "emp_id";
this.lstOne.DataBind();

this.lstTwo.DataSource = lstOne.Items;
this.lstTwo.DataTextField = "Text";
this.lstTwo.DataValueField = "Value";
this.lstTwo.DataBind();
}

HTH.

Thanks & Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

xmldatareader? 3
LINQ and blobs 4
datareader - connection leaks in DB 6
Query returns nothing 2
Binary images - thumbnails 4
Guid handling 1
DropDownList Selection Menu 3
run test code 4

Top