Tuesday, October 2, 2012

Bind DropDownList from DataBase , Using c#


string s = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

private void bindtoddl()
    {
        SqlConnection con = new SqlConnection(s);
        SqlCommand cmd = new SqlCommand("select FieldName from TableName", con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "FieldName";                            // FieldName of Table in DataBase
        DropDownList1.DataValueField = "FieldName";
        DropDownList1.DataBind();
        
      }        

No comments:

Post a Comment