Drop down list not binding with sqldatareader

前端 未结 4 1334
一个人的身影
一个人的身影 2021-01-24 10:15

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,          


        
相关标签:
4条回答
  • 2021-01-24 10:48

    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.

    0 讨论(0)
  • 2021-01-24 10:49

    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.

    0 讨论(0)
  • 2021-01-24 10:53

    This seems clear postback problem.

    Bind your drop down on !postback.

    Eg.

    if(!IsPostBack)    
     {
       populateDdl();
     }
    
    0 讨论(0)
  • 2021-01-24 10:57

    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();
    
    0 讨论(0)
提交回复
热议问题