Drop down list not binding with sqldatareader

随声附和 提交于 2019-12-02 04:41:51

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();

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!