public void Cmbfill(DropDownList Cntrl,string Sql)
{
Cmd=new SqlCommand(Sql,con);
Cmd.CommandTimeout=0;
myReader=Cmd.ExecuteReader();
Cntrl.Items.Clear();
if (myReader.HasRows)
{
while (myReader.Read())
{
ListItem Lst2=new ListItem(myReader[1].ToString(),myReader[0].ToString());
Cntrl.Items.Add(Lst2);
}
myReader.Close();
}
else
{
myReader.Close();
}
}
this our simple code in filling dropdownlist in web forms...
i nedd exact coding in C# for windows application. & and one more datagridview control details in fullstrngth
pls help me?
Loading
RujutaPosted Nov 21, 2009, 6:51 AM
The code will be
SqlCommand cmd = new SqlCommand("SELECT * FROM [tblOne]", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
cmd.Connection.Open();
SqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();
DropDownList1.DataSource = ddlValues;
DropDownList1.DataValueField = "theName";
DropDownList1.DataTextField = "theName";
DropDownList1.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
naura paxPosted Oct 29, 2009, 1:09 AM
I have an alternate solution:
public void Cmbfill(DropDownList Cntrl,string Sql,bool valFieldRequired,string dispField,string valField)
{
Cmd=new SqlCommand(Sql,con);
Cmd.CommandTimeout=0;
DataSet ds =Cmd.ExecuteDataSet();
Cntrl.DataSource=null;
if(typeof(Cntrl)==ComboBox){
Cntrl.DisplayMember = dispField;
if (valFieldRequired)
Cntrl.ValueMember = valField; }
Cntrl.DataSource = ((DataSet)ds).Tables[0];
}