Drop down list not binding with sqldatareader

前端 未结 4 1349
一个人的身影
一个人的身影 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: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.

提交回复
热议问题