how to populate values in dropdownlist from database using asp.net

Advertisement
In this article we can able to see  how to Populate values in to drop down from Data Base using asp.net

 public void loadDdlRoles()
        {

            string constr = ConfigurationManager.ConnectionStrings["DConnectionString"].ToString(); // connection string
            SqlConnection con = new SqlConnection(constr);
            con.Open();

            SqlCommand com = new SqlCommand("select *from dbo.aspnet_Roles", con); // table name 
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);  // fill dataset
            DdlRoles.DataValueField = ds.Tables[0].Columns["RoleName"].ToString();             // to retrive specific  textfield name 
            DdlRoles.DataSource = ds.Tables[0];      //assigning datasource to the dropdownlist
            DdlRoles.DataBind();  //binding dropdownlist


        }
Don't Forget to call that method in Page load

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadDdlRoles();
          }

        }
Keep always Smile:)
Advertisements
SHARE

Unknown

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a comment