i have a form with a collection of about five drop down . i have my query as follows .
string sql = \"SELECT a.clientID ,a.[cname],b.bid,b.[bname],c.contactID,
Your choices are to either rerun/refill it or create separate readers or better yet fill a datatable instead and then you can reuse the datatable.
Reader is readonly
and forward only that's why only first dropdonw
get filled with data and others are empty.
You can use datset
or Datatable
for same problem .
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
Dataset dsresult = cmd.ExecuteDataset();
If(dsResult !=null)
{
if(dsResult.Rows.count>0)
{
drClient.Enabled = true;
drClient.DataSource = dsResult.Tables[0] ;
drClient.DataTextField = Convert.ToString(ds.Tables[0].Columns["cname"]);
drClient.DataValueField = ds.Tables[0].Columns["clientID"] ;
drClient.DataBind();
}
}
Datareader
is connected architecture needs continuous connection and fetches one row at a time in forward mode better use dataset
which uses disconnected architecture
and can be used for retrieving data multiple times.
This seems clear postback problem.
Bind your drop down on !postback.
Eg.
if(!IsPostBack)
{
populateDdl();
}
Either you will have to make a seperate reader for each binding
or you can do this by filling a datatable ( i would prefer this). Like,
DataTable dt = new DataTable();
using (SqlDataAdapter a = new SqlDataAdapter(sql, connection))
{
a.Fill(dt);
}
drClient.DataSource = dt;
drClient.DataBind();
drBranch.DataSource = dt;
drBranch.DataBind();
drContact.DataSource = dt;
drContact.DataBind();
drFax.DataSource = dt;
drFax.DataBind();