Gautam Buddha Temple Varanasi,Varanasi - Buddhist Pilgrimage

Sunday, October 10, 2010

Bind DropDownList using SqlDataReader

This code pulling out data from a database into a data reader is as follows.
 protected void Page_Load(object sender, EventArgs e)  
{
if (!Page.IsPostBack)
{
bindDDL();
}
}
Public void bindDDL()
{
SqlConnection con =new SqlConnection(ConfigurationManager.
ConnectionStrings["ConnectionString"].ConnectionString.ToString();)
SqlCommand cmd=new SqlCommand();
cmd.connection=con;
con.open();
cmd.commandText="SELECT Citiid,CitiName FROM tableCity";
SqlDataReader dr;
dr=cmd.ExecuteReader();
ddlname.DataSource =dr;
ddlname.DataTextField = "CitiName";
ddlname.DataValueField = "Citiid";
ddlname.DataBind();
dr.close();
con.close();
}

Thanks & Regards
Santosh

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. hi i m struggling to bind drop-downlist to database. My drop-down list is in grid view. i tried several find control non are working. Also i want to store result in string from the select value of drop-down...



    Please.

    ReplyDelete
  3. @tarun u can find out control dropdown list on grid event rowdata bound then using above code u can bind dropdown control which is inside gridview

    ReplyDelete